Skip to content

Instantly share code, notes, and snippets.

@mitchty
Forked from peterc/irb3.rb
Created December 4, 2011 05:36
Show Gist options
  • Save mitchty/1429309 to your computer and use it in GitHub Desktop.
Save mitchty/1429309 to your computer and use it in GitHub Desktop.
irb3 - Run an IRB-esque prompt over multiple Ruby implementations at once using RVM
#!/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 RVM)
#
# By Peter Cooper, BSD licensed
#
# Main dependency is term-ansicolor for each impl:
# rvm exec gem install term-ansicolor
#
# 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'
require 'rvm'
require 'yaml'
include Term::ANSIColor
MYCONFIG = ENV['HOME'] + File::Separator + '.irb3.yaml'
ALL_VERSIONS = RVM::Environment.new.list_strings
BASIC_VERSIONS = %w{1.8.7 1.9.3}
unless File.exists? MYCONFIG
puts "No #{MYCONFIG} file found, creating a default file."
File.open(MYCONFIG, 'w').write(Hash[:default_rubies => BASIC_VERSIONS].to_yaml)
end
config = YAML.load_file(MYCONFIG)
DEFAULT_VERSIONS = ALL_VERSIONS.dup.keep_if {|l|
config[:default_rubies].collect {|r| l !~ /#{r}/}.include? false
}
loop do
# Read lines
lines = []
begin
line = Readline::readline('> ')
print reset
exit if line.nil? or line =~ /^(exit|quit)$/i
Readline::HISTORY << line
line.chomp!
lines << line
end while line =~ /(\s+|\\)$/
# Determine versions to run
versions = case line
when /\#\s*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'
require 'term/ansicolor'
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.blue + "!! \#{e.class}: \#{e.message}" + Term::ANSIColor.reset
end
print o}
f.close
versions.each do |version|
result = `rvm #{version} exec ruby #{f.path}`
puts "#{version} => #{result}"
end
end
@mitchty
Copy link
Author

mitchty commented Dec 4, 2011

A few enhancements to make it use a dotfile for what to run, and to use rvm to parse out all of the rubies installed for # all runs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment