Created
July 11, 2012 07:51
-
-
Save shireeshj/3088818 to your computer and use it in GitHub Desktop.
irb3 - Run an IRB-esque prompt over multiple Ruby implementations at once using rvm
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 | |
# | |
# This is a modified version of irb3 by Peter Cooper. This uses RVM instead of rbenv | |
# irb3 - Runs an IRB-esque prompt (but it's NOT really IRB!) over multiple | |
# versions of Ruby at once (using rvm) | |
# Contributor: Shireesh Jayashetty ( elitmus.com ) | |
# | |
# Original version: https://gist.github.com/1229225 | |
# 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 = %x[$rvm_path/bin/rvm list strings].split | |
DEFAULT_VERSIONS = %w{ruby-1.8.7-p358 ruby-1.9.2-p320} | |
ruby_binaries = {} | |
gem_binaries = {} | |
ALL_VERSIONS.each do |r| | |
ruby_binaries["#{r}"] = %x[bash -c -l "rvm info #{r} binaries | grep 'bin/ruby'"].gsub(/^\s*ruby:\s*"/,"").gsub(/"\n/,"") | |
end | |
ALL_VERSIONS.each do |r| | |
gem_binaries["#{r}"] = %x[bash -c -l "rvm info #{r} binaries | grep 'bin/gem'"].gsub(/^\s*ruby:\s*"/,"").gsub(/"\n/,"") | |
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 = "\#{gem_binary[version]} 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_binaries[version] | |
result = red + "\n Missing, run: rvm install #{version}; #{gem_binaries[version]} install term-ansicolor" + reset | |
else | |
result = `#{ruby_binaries[version]} #{f.path}` | |
end | |
rv = version.split("-") | |
puts " #{rv[1]} (#{rv[2]}) => #{result}" | |
end | |
puts | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment