First Ruby Program
Learn to create and execute the first Ruby program.
Problem solving with Ruby
The basic tools for understanding the flow and the results of simple programs are
Note: We can rerun a program as many times as we want, to try outputs for different inputs or values of variables.
In Ruby, we use gets
for input (to read a value from the user).
Hello World
It’s time to write our first program in Ruby, which will simply display the message “Hello World!”
on the screen.
This is the code widget that we’ll use to write our Ruby code. We can execute our Ruby program by clicking the “Run” button at the bottom of the widget:
print "Hello World!" # Printing Hello World!
Note: We can add non-executable text in a Ruby program by adding a hash sign (
#
) at the start of the text. This type of text is called a comment. We use comments to include descriptions in the program.
The program below is the same as the one above and has the same output. The only exception is that we use parentheses with the print
statement in the below program. Using ()
with print
does not affect the output of the code, but it does make it easy to read.
print("Hello World!") # Printing Hello World! while using parentheses with print
Add a new line in the output
We can break a sentence into multiple sentences by using "\n"
in the print()
statement. The string, "\n"
, introduces a new line when used in the print statement.
print("Single")print("Line\n")print("Separate New Line\n")
Personalize messages and add variables
In the following program, we store a string in a variable, name
, and output it using a print()
statement inside #{}
. We can use #{}
to output variables in the print statement. The operator, #{}
, performs expression substitution in the string and is not part of the output itself.
Note: A variable name can only contain letters (
A
–Z
anda
–z
) and the underscore symbol (_
). It may also contain digits (0
–9
), but the variable name can’t start with those. For example,Key_2
is a valid variable name but2_key
is not. Variable names are case-sensitive, meaning thatname
,Name
, andNAME
are three different variables.
name = "Educative"print("Hello User. Welcome to #{name}")
Let’s write another Ruby program that inputs the name of the user before displaying a greeting message. We use the name
as a variable to store the input given by the user in Ruby.
main.rb
in the left pane of the following widget is the name of the program file. .rb
indicates that it’s a Ruby program. First, we print the text, "Enter your name: "
, using the print()
statement as a prompt and gets
to take input. When we run this program, it shows a black screen called the terminal, where we can input values and see the resulting output:
print("Enter your name: ") name = gets # Taking input by user in variable name print("Hello #{name} Welcome to Educative\n") # Printing User's name with Hello
The word, exit
, at the end of the output indicates the completion of the program in the terminal.
The code above displays the message, "Hello"
, before the value of the name variable on the first line. Then, it displays “Welcome to Educative”
on the second line.
In the code widget above, when we take input with only the gets
statement, it will break the line when used in the print statement. We can use gets.chomp
to avoid the line break.
print("Enter your name: ") name = gets.chomp # Using chomp with the user input to avoid line-break. print("Hello #{name} Welcome to Educative\n")
The code above displays the output in the same line.
Ruby programming practice
Let’s practice writing the first Ruby program that takes multiple inputs from the user. Keep the following tips in mind.
Tips to write cleaner code:
- We can add the parentheses
()
with- Using
"\n"
can add a new line in the code.- We can use
#{}
to output variables inprint()
.- We can use
gets.chomp
to avoid the line break.
Tell a story
Let’s write a program that tells a story with the information given by the user. The program should ask the user to enter their name, age, city, college, profession, and pet’s name.
We should also keep in mind that we use the same order of inputs in our code. The program should display the following story, inserting the user’s input into the appropriate locations in the following text:
There once was a person named NAME who lived in CITY. At the age of AGE, NAME went to college at COLLEGE. NAME graduated and went to work as a PROFESSION. Then, NAME adopted an animal named PETNAME. They both lived happily ever after!
# Write your code here