Skip to content

Welcome to Tech by Example

Menu
  • Home
  • Posts
  • System Design Questions
Menu

Run Ruby language code in command line or terminal – Single-line and Multiline

Posted on August 7, 2021August 7, 2021 by admin

Overview

It is possible to run a ruby code directly on a command line or the terminal itself.  We need to pass the -e flag. This is the syntax for the same

ruby -e "<ruby_code>"

Program

Below is one simple example

ruby -e "puts 'hello'"

Run the above command on the terminal and it will give the output

hello

How to run the multiline ruby code. Below is an example of the same. You don’t need to use -e flag.

ruby <<END
 puts "Start"
 5.times do |i|
   puts i
 end
 puts "End"
END

The above program will output

Start
0
1
2
3
4
End
  • ruby
  • ©2025 Welcome to Tech by Example | Design: Newspaperly WordPress Theme