Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find the ruby interpreter?

Tags:

ruby

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!

like image 411
docwhat Avatar asked May 11 '10 20:05

docwhat


People also ask

How do I run Ruby interpreter?

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.

Does Ruby have an interpreter?

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).

How does Ruby interpreter work?

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.

What is embedded Ruby interpreter?

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.


1 Answers

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"
like image 128
rogerdpack Avatar answered Sep 26 '22 02:09

rogerdpack