Write and Execute your first Ruby program using Mac Terminal

Follow below steps to run your first Ruby program on Mac Terminal:

Step 1: You can watch our previous video if Ruby is not installed on your system. To check ruby is installed on your system or not; type below command:

$ ruby -v

This will return the version of ruby installed on your system. 

Step 2: Now, you need to locate your project directory or navigate to your desired location. To create new directory, type mkdir followed by directory name

$ mkdir rubydemo
$ cd rubydemo

Step 3: Type below command to create file using nano text editor

$ nano hello.rb

Step 4: In editor file add following code and save the file with hello.rb , here hello is my filename and .rb is it's extension.

puts "Hello World!"

Any characters that are inside of quotation marks are called a string, so here "Hello World!" is a String. The puts method will print this string to the screen when we will execute code.

Step 5: Execute the code by typing below ruby command, here hello.rb is our file name 

$ ruby hello.rb

Once you will execute above command content inside hello.rb will be evaluated and executed and result will be our string "Hello World!"

Mon, 06/21/2021 - 21:11

Authored by

Tarun Goswami, works as a Technical Lead at the well-known IT services company. With Over 11+ years of experience in Quality Assurance, he writes engaging and informative blogs in his spare time.