Skip to content

Instantly share code, notes, and snippets.

@masover
Created January 17, 2009 19:16
Show Gist options
  • Save masover/48420 to your computer and use it in GitHub Desktop.
Save masover/48420 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby1.8
# Method chosen carefully -- avoiding symbols
def unshift env, value
ENV[env] = ENV[env].to_s.split(':').unshift(value).join(':')
end
def read
homedir = '/home/rubygems'
gem = "#{homedir}/home"
unshift 'GEM_HOME', "#{homedir}/home"
unshift 'PATH', "#{homedir}/bin:#{gem}/bin"
lib = "#{homedir}/lib"
unshift 'RUBYLIB', lib
program = ARGV.shift
real_path = (program =~ /\//) ? program : `which #{program}`.chomp
possible_shebang = File.read(real_path, 1024).split("\n").first
if possible_shebang =~ /^#!/
parts = possible_shebang.split
parts.shift if parts.first =~ /env$/
if parts.first =~ /ruby(\d+(\.\d+)*)?$/
# Found a Ruby file. Clean up and return.
[:unshift, :read].each do |m|
self.class.send :remove_method, m
end
$:.unshift lib
return $0 = real_path
end
end
# Not a Ruby file, then
exec program, *ARGV
end
# If we get a return value (program didn't exit), we execute that file
# in the current Ruby interpreter. Because of the names of the above methods,
# we actually have ZERO footprint beyond changing environment variables
# (and changing which interpreter to use).
load read
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment