📚 Scripting Languages Tutorial

Scripting Languages

Learn scripting languages (Ruby, Perl, TCL and Tk) with simple explanations, examples, techniques and practical notes.

← Back to Scripting Languages

Input and output statements in Ruby

Input and output statements are important features of every programming language. They allow a program to communicate with the user.

In Ruby, input statements are used to accept data from the user, whereas output statements are used to display information on the screen.

Output Statements

Ruby provides the following methods to display the output to the screen. Those are,

  1. puts
  2. print
  3. printf

1. puts method

The puts method is used to display output on the screen. It automatically moves the cursor to the next line after displaying the output, i.e. it adds a newline at the end.

Syntax:
puts "message"
Example:
# displaying text
puts "Welcome to Ruby Programming"
puts "Student details"

# displaying variables
sname = "Naveen"
age = 22
puts sname
puts "Age is #{age}"

# displaying multiple values
puts sname, age
Output:
Welcome to Ruby Programming
Student details
Naveen
Age is 22
Naveen
22

Note: #{age} inserts the variable value into the output string.

2. print method

The print method is used to display output on the screen without adding a newline at the end, i.e. this method works similarly to puts, but does not add a newline.

Syntax:
print "message"
Example:
# displaying text
print "Welcome "
print "to Ruby\n"

# displaying variables
name = "Madhu"
age = 21
print "Name: "
print name

# displaying multiple values
print "\nAge: ", age
Output:
Welcome to Ruby
Name: Madhu
Age: 21

3. printf method

The printf method is used to display formatted output. It allows programmers to control how numbers and strings are displayed.

Syntax:
printf ("format_specifier", values)

Some commonly used format specifiers are:

Format specifier Meaning Example
%sString"Ruby"
%dInteger100
%fFloating-point number25.5420
%.2fFloating-point number with 2 decimal places25.54
%cCharacter'A'
Example:
name = "Madhu"
age = 21
marks = 89.567
printf ("Name: %s\n", name)
printf ("Age: %d\n", age)
printf ("Marks: %0.2f\n", marks)
Output:
Name: Madhu
Age: 21
Marks: 89.57

Input Statements

Ruby provides the following methods to read input from the user. Those are,

  1. gets
  2. gets.chomp

1. gets method

The gets method reads a line of input entered by the user. By default, the gets method reads the input as a string. The gets method normally retains the newline character (\n) entered when the user presses Enter.

Syntax:
variable_name = gets
Example:
puts "Enter your name:"
name = gets
puts "Welcome #{name} !"
Output:
Enter your name:
Madhu
Welcome Madhu
 !

2. gets.chomp method

The chomp method is used to remove the trailing newline character from the input received through gets.

Syntax:
variable_name = gets.chomp
Example:
puts "Enter your name:"
name = gets.chomp
puts "Welcome #{name} !"
Output:
Enter your name:
Madhu
Welcome Madhu !

Reading Numeric Input in Ruby

As we know, the gets method reads input as a string. If we want to perform mathematical operations, we need to convert the input into an appropriate numeric data type.

Ruby provides the following methods to convert string inputs to integers and floats. Those are,

  • to_i : used to convert the input into an integer
  • to_f : used to convert the input into a float
Syntax:
variable_name = gets.chomp.to_i
variable_name = gets.chomp.to_f
Example:
puts "Enter your age:"
age = gets.chomp.to_i
puts "Enter your salary:"
salary = gets.chomp.to_f
puts "Your age is #{age}"
puts "Your salary is #{salary}"
Output:
Enter your age:
24
Enter your salary:
25123.42
Your age is 24
Your salary is 25123.42