Module: Multirb

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

Constant Summary collapse

ALL_VERSIONS =
%w{jruby 1.8.7 1.9.2 1.9.3 2.0.0}
DEFAULT_VERSIONS =
%w{1.8.7 1.9.3 2.0.0}
VERSION =
"0.0.1"

Instance Method Summary collapse

Instance Method Details

#create_and_save_code(lines) ⇒ Object



55
56
57
58
59
60
# File 'lib/multirb.rb', line 55

def create_and_save_code(lines)
  f = Tempfile.new('multirb')
  f.puts create_code(lines)
  f.close
  f.path
end

#create_code(lines) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/multirb.rb', line 36

def create_code(lines)
  %{# encoding: utf-8
    ARRAY = ['a', 'b', 'c']
    HASH = { :name => "Jenny", :age => 40, :gender => :female, :hobbies => %w{chess cycling baking} }
    ARRAY2 = [1,2,3]
    STRING = "Hello"
    STRING2 = "çé"
    STRING3 = "ウabcé"
    o = begin
      "\e[32m" + eval(<<-'CODEHERE'
        #{lines.join("\n")}
      CODEHERE
      ).inspect + "\e[0m"
    rescue Exception => e
      "\e[31m!! \#{e.class}: \#{e.message}\e[0m"
    end
    print o}
end

#determine_versions(lines, defaults = DEFAULT_VERSIONS) ⇒ Object



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

def determine_versions(lines, defaults = DEFAULT_VERSIONS)
  defaults = DEFAULT_VERSIONS if defaults.empty?
  case lines.last
  when /\#\s?all$/
    ALL_VERSIONS
  when /\#\s?(.+)$/
    [*$1.strip.split(',').map(&:strip)]
  else
    defaults
  end
end

#read_linesObject



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

def read_lines
  lines = []
  begin
    print "\e[0m"
    line = Readline::readline("> ")
    Readline::HISTORY << line
    line.chomp!
    lines << line
    exit if line =~ /^exit$/i
  end while line =~ /\s+$/
  lines
end

#run_code(filename, version, out = STDOUT) ⇒ Object



62
63
64
# File 'lib/multirb.rb', line 62

def run_code(filename, version, out = STDOUT)
  out.puts "  #{version} => #{`rvm #{version} exec ruby #{filename}`}"
end