How to run Ruby code from terminal?

RubyConsoleTerminal

Ruby Problem Overview


I need to run a few lines of Ruby code from terminal, but I can't find the needed parameter for it.

Can you explain how to do this?

Ruby Solutions


Solution 1 - Ruby

If Ruby is installed, then

ruby yourfile.rb

where yourfile.rb is the file containing the ruby code.

Or

irb

to start the interactive Ruby environment, where you can type lines of code and see the results immediately.

Solution 2 - Ruby

You can run ruby commands in one line with the -e flag:

ruby -e "puts 'hi'"

Check the man page for more information.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndresh PodzimovskyView Question on Stackoverflow
Solution 1 - RubytheglauberView Answer on Stackoverflow
Solution 2 - RubyLanguagesNamedAfterCofeeView Answer on Stackoverflow