rubymacosunixterminal

Launching Ruby without the prefix "Ruby"


I'm on OS X (with bash) and a newbie at unix. I want to know if it's possible to amend some file such that to run a ruby program, I don't need "ruby file.rb", but instead can just run "ruby.rb".

Is there a reason NOT to do this?

Thanks!


Solution

  • Yes you can do this.

    Assuming ruby.rb has something like this in it:

    #!/usr/bin/env ruby
    puts 'Hello world'
    

    At the command line: chmod +x ruby.rb

    This makes it executable.

    Then you can execute it like this:

    ./ruby.rb
    

    EDIT (Jörg W Mittag): Using #!/usr/bin/env ruby instead of #!/usr/bin/ruby as the shebang line is more portable, because on every Unix produced in the last 20 years, the env command is known to live in /usr/bin, whereas Ruby installations are typically all over the place. (E.g., mine lives in /home/joerg/jruby-1.2.0/bin/ruby.)