Module: WithRuby

Defined in:
lib/with_ruby.rb,
lib/with_ruby/version.rb

Defined Under Namespace

Modules: Version

Class Method Summary collapse

Class Method Details

.build(command, options) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/with_ruby.rb', line 24

def build(command, options)
  "require 'rubygems'\n" +
  "require 'yaml'\n" +
  "def __with_ruby\n" +
  build_params(options) +
  command +
  "end\n" +
  "begin\n" +
  "  STDOUT.puts(YAML.dump(__with_ruby))\n" +
  "rescue Exception => boom\n" +
  "  STDOUT.puts(YAML.dump(boom))\n" +
  "end\n" +
  "__END__\n"
end

.build_params(options) ⇒ Object



39
40
41
42
43
# File 'lib/with_ruby.rb', line 39

def build_params(options)
  options.map do |key, val|
    "#{key} = YAML.load(%q{#{YAML.dump(val)}})"
  end.join("\n")
end

.run(options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/with_ruby.rb', line 9

def run(options = {})
  bin = options.delete('bin') || '/usr/bin/env ruby -Ku'
  
  command = yield.to_s
  
  ret = nil
  IO.popen(bin, 'r+') do |io|
    io.puts(build(command, options))
    ret = YAML.load(io.read)
  end
  raise(ret) if ret.is_a?(Exception)

  ret
end