Create and run First program example code

Hello World program is a first step to learn any programming language Steps to write a Hello World First program code.

To learn Ruby programming, You need to have any basic programming experience.

  • Create a Ruby file helloworld.rb
  • Write a code to print hello world to console
  • Compile and run the hello world program

Open any text editor and create file called hellowrold.rb, Here rb is an extension used for creating ruby program.


In helloworld.rb program, write a below lines of code and save the file

puts "Hello world first program"

puts is an function or method which accepts string or ruby expression displayed to console. In Ruby Strings are enclosed in double or single quotation marks

Next step is to compile and run the program.

Open termina or command line.

ruby helloworld.rb

ruby is an interpreter to run the ruby program code. It accepts name of ruby file After executing the command, It outputs string to console.

Hello world first program

How to read command line output and print to console in Ruby?

This program read he

puts "Please enter your Age."
age = gets
puts "Your Age is #{age}"

Here are sequence of steps

  • First print the string to take input from an user. puts method is used here
  • Need to take input from an user, gets method used and waits for the program to read user keyboard
  • Create a variable age and assign the entered value after enter command
  • Finally print the user age using string interpolation syntax.

After running the above program, Output is

Please enter your Age.
Your Age is 30