/home/kirti/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in
`require’: cannot load such file – dir (LoadError)
Why the line is needed - #!/usr/bin/env ruby ? I have seen lots of
code having that line at top. I am not aware of that one.
Could you help me ?
The #! line (shebang line) specifies which interpreter to use. So a
script starting with
#!/usr/bin/ruby
would always use the /usr/bin/ruby interpreter to run the script.
As I use chruby to let myself switch between ruby scripts the ruby I
want to run for my programs is pointed at by
/Users/mike/.rbenv/shims/ruby, whose directory is on my PATH. When I
start a script with
#!/usr/bin/env ruby
then the env program searches my PATH for the first ruby it finds and
uses that to interpret the script. This is useful to me as it lets me
easily switch versions in my shell and have the ruby version I’m
interested in execute the script.
It also helps when distributing a script for use on systems where you
don’t know if ruby is /bin/ruby, /usr/local/bin/ruby, /usr/bin/ruby, or
something else - as long as the PATH is set up correctly ruby will be
found.
There is a downside to /usr/bin/env in that it depends on the path of
the user running it, so the same script on a system run by two different
users can use different interpreters. That can cause confusion when
debugging.
The #! line (shebang line) specifies which interpreter to use. So a
script starting with
#!/usr/bin/ruby
would always use the /usr/bin/ruby interpreter to run the script.
Good explanation…
So suppose I am having Ruby 1.9.3-p374, Ruby2.0.0-p0 are installed. Now
is the line #!/usr/bin/ruby would also be able to the desired version
as I want ? If no then how can I run the same script with different
version of Ruby(s)?
So suppose I am having Ruby 1.9.3-p374, Ruby2.0.0-p0 are installed. Now
is the line #!/usr/bin/ruby would also be able to the desired version
as I want ? If no then how can I run the same script with different
version of Ruby(s)?
Thanks as always!!
Using rbenv:
ratdog:tmp mike$ ls -l try.rb
-rwxr-xr-x 1 mike mike 71 22 Jun 14:02 try.rb
ratdog:tmp mike$ cat try.rb
#!/usr/bin/env ruby
puts “I’m running under #{RUBY_VERSION}”
END
ratdog:tmp mike$ rbenv version
2.0.0-p195 (set by /Users/mike/.rbenv/version)
ratdog:tmp mike$ ./try.rb
I’m running under 2.0.0
ratdog:tmp mike$ rbenv shell 1.9.3-p429
ratdog:tmp mike$ rbenv version
1.9.3-p429 (set by RBENV_VERSION environment variable)
ratdog:tmp mike$ ./try.rb
I’m running under 1.9.3
So the same script runs under different interpreters depending on which
version I have set to execute using rbenv.