119

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?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Andresh Podzimovsky
  • 1,511
  • 4
  • 13
  • 17
  • Possible duplicate of [How to execute a Ruby script in Terminal?](https://stackoverflow.com/questions/8721369/how-to-execute-a-ruby-script-in-terminal) – ymoreau Aug 17 '18 at 08:56
  • You can use online IDE to run your code. That's possible write code or upload file. https://repl.it/languages/ruby – Darlan Dieterich May 14 '19 at 14:48

2 Answers2

204

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.

Raj
  • 22,346
  • 14
  • 99
  • 142
theglauber
  • 28,367
  • 7
  • 29
  • 47
62

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

ruby -e "puts 'hi'"

Check the man page for more information.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
LanguagesNamedAfterCofee
  • 5,782
  • 7
  • 45
  • 72