Create and run First program example code

Hello World program is the first step to learning 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 the console
  • Compile and run the Hello World program

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

helloworld.rb

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

puts "Hello world first program"

puts is a function or method that accepts string or ruby expressions displayed to the console. In Ruby Strings are enclosed in double or single quotation marks

The next step is to compile and run the program.

Open the terminal or command line.

ruby helloworld.rb

ruby is an interpreter to run the ruby program code. It accepts the name of the ruby file After executing the command, It outputs a string to the 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 a user. The puts method is used here
  • Need to take input from a user, gets method used and waits for the program to read user keyboard
  • Create a variable age and assign the entered value after the enter command
  • Finally print the user age using string interpolation syntax.

After running the above program, the Output is

Please enter your Age.
30
Your Age is 30