Skip to main content

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!"

Submitted by tgoswami on June 21, 2021

Tarun has 11+ years of experience in Quality Assurance in different domains like Banking, E-commerce, Health and Education.


At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.