-
-
Save ashleyw/1229225 to your computer and use it in GitHub Desktop.
irb3 - Run an IRB-esque prompt over multiple Ruby implementations at once using rbenv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# encoding: utf-8 | |
# irb3 - Runs an IRB-esque prompt (but it's NOT really IRB!) over multiple | |
# versions of Ruby at once (using rbenv) | |
# | |
# By Peter Cooper, BSD licensed | |
# | |
# 1. Type in expressions and press enter. | |
# 2. Leave space on the end of lines to enter more lines. | |
# 3. Add # all to run all versions, nothing for default | |
# 4. Add # version,version,version to run specific versions. | |
# 5. exit on its own to exit (or Ctrl+D) | |
require 'readline' | |
require 'term/ansicolor' | |
require 'tempfile' | |
include Term::ANSIColor | |
ALL_VERSIONS = `rbenv versions --bare`.split | |
DEFAULT_VERSIONS = %w{1.8.7-p352 1.9.2-p290} | |
def ruby_dir(version) | |
`rbenv prefix #{version}`.gsub("\n", "") | |
end | |
def ruby_binary(version) | |
"#{ruby_dir(version)}/bin/ruby" | |
end | |
loop do | |
# Read lines | |
lines = [] | |
begin | |
line = Readline::readline('> ' + bold) | |
print reset | |
Readline::HISTORY << line | |
line.chomp! | |
lines << line | |
exit if line =~ /^exit|quit$/i | |
end while line =~ /\s+$/ | |
# Determine versions to run | |
versions = case line | |
when /\# all$/ | |
ALL_VERSIONS.dup | |
when /\#\s(.*)$/ | |
[*$1.strip.split(',').map(&:strip)] | |
else | |
DEFAULT_VERSIONS.dup | |
end | |
# Create code | |
f = Tempfile.new('irb3') | |
f.puts %{# encoding: utf-8 | |
require 'rubygems' | |
begin | |
require 'term/ansicolor' | |
rescue LoadError | |
version = "\#{RUBY_VERSION}-p\#{RUBY_PATCHLEVEL}" | |
command = "\#{`rbenv prefix \#{version}`.gsub("\n", "")}/bin/gem install term-ansicolor" | |
puts "'term-ansicolor' not installed, run this:\n \#{command}" | |
exit | |
end | |
ARRAY = ['a', 'b', 'c'] | |
HASH = { :name => "Fred", :age => 40, :gender => :male } | |
ARRAY2 = [1,2,3] | |
STRING = "Hello" | |
STRING2 = "çé" | |
STRING3 = "ウabcé" | |
o = begin | |
Term::ANSIColor.green + eval(<<-'STUFFHERE' | |
#{lines.join("\n")} | |
STUFFHERE | |
).inspect + Term::ANSIColor.reset | |
rescue Exception => e | |
Term::ANSIColor.red + "!! \#{e.class}: \#{e.message}" + Term::ANSIColor.reset | |
end | |
print o} | |
f.close | |
versions.each do |version| | |
unless File.exists? ruby_binary(version) | |
result = red + "\n Missing, run: rbenv install #{version}; #{ruby_dir(version)}/bin/gem install term-ansicolor" + reset | |
else | |
result = `#{ruby_binary(version)} #{f.path}` | |
end | |
puts " #{version.split("-")[0]} => #{result}" | |
end | |
puts | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment