Page 1 of 1

ruby 1.8/1.9 switcher script

PostPosted: Sun Feb 15, 2009 6:34 pm
by shawn42
Not for use by the faint of heart, but this may help some of those transitioning to ruby 1.9 that still want to be able to switch back to ruby1.8.

sudo setruby.rb -v
shows current version

sudo setruby.rb 1.8
switches to 1.8

sudo setruby.rb 1.9
switches to 1.9

Code: Select all
#!/usr/bin/env ruby
# script to toggle between ruby 1.8 and ruby 1.9
RUBY_ONE_EIGHT = [
'/usr/local/bin/gem1.8',
'/usr/local/bin/gem_mirror1.8',
'/usr/local/bin/gem_server1.8',
'/usr/local/bin/gemlock1.8',
'/usr/local/bin/gemri1.8',
'/usr/local/bin/gemwhich1.8',
'/usr/local/bin/irb1.8',
'/usr/local/bin/rake1.8',
'/usr/local/bin/ri1.8',
'/usr/local/bin/ruby1.8',
'/Users/sanderson/bin/rsdl1.8'
]

RUBY_ONE_NINE = [
'/usr/local/bin/gem1.9',
'/usr/local/bin/irb1.9',
'/usr/local/bin/ri1.9',
'/usr/local/bin/ruby1.9',
'/usr/local/bin/rake1.9',
'/Users/sanderson/bin/rsdl1.9'
]

VERSIONS = {
  '1.8' => RUBY_ONE_EIGHT,
  '1.9' => RUBY_ONE_NINE
}

target_version = ARGV[0]
if target_version.nil?
  version = `ruby -v`.strip
  puts "CURRENT VERSION IS: #{version}"
  exit 0
end

for ver,files in VERSIONS.select{|k,v|k!=target_version}
  for file in files
    symlink = file.chomp(ver)
    File.delete(symlink) if File.exist?(symlink)

  end
end

for ver,files in VERSIONS.select{|k,v|k==target_version}
  for file in files
    symlink = file.chomp(ver)
    File.delete(symlink) if File.exist?(symlink)
    File.symlink(file,symlink) if File.exist?(file)
  end
end