Inside a ruby script, how do I get the path to the ruby interpreter?
Example script:
#!/path/to/ruby
puts `#{RUBY_INTERPRETER_PATH} -e "puts 'hi'"`
#EOF
Where RUBY_INTERPRETER_PATH
is a mythical way of finding /path/to/ruby
.
This is just an example, though. I realize in this case that I could just copy /path/to/ruby
into the script, but I don't want to do that. I want this to work "correctly" regardless of what the #!
line says. Even if running under windows.
Ciao!
Open Interactive Ruby Open up IRB (which stands for Interactive Ruby). If you're using macOS open up Terminal and type irb, then hit enter. If you're using Linux, open up a shell and type irb and hit enter. If you're using Windows, open Interactive Ruby from the Ruby section of your Start Menu.
As of Ruby 2.6, YARV remains the official interpreter technology used for Ruby. When you run a Ruby program, YARV translates the code to a limited instruction set that can run in the Ruby virtual machine (VM).
During the parsing stage, Ruby transforms the text into something called an abstract syntax tree, or AST. The abstract syntax tree is a representation of your program in memory. You might say that programming languages in general are just more user-friendly ways of describing abstract syntax trees.
eRuby stands for embedded Ruby. It's a tool that embeds fragments of Ruby code in other files such as HTML files similar to ASP, JSP and PHP. eRuby allows Ruby code to be embedded within (delimited by) a pair of <% and %> delimiters.
These days (1.9+) you can use built-in methods (which are supposed to work with Jruby, etc.) like this:
RbConfig.ruby or Gem.ruby
$ irb --simple-prompt
>> RbConfig.ruby
=> "C:/installs/Ruby193/bin/ruby.exe"
>> Gem.ruby
=> "C:/installs/Ruby193/bin/ruby.exe"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With