📚 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

The structure and Execution of Ruby Programs

A Ruby program is a set of instructions written using the Ruby programming language to perform a particular task.

Ruby programs are generally stored in files with the .rb extension. These files contain Ruby statements, variables, methods, classes, objects and control structures.

Ruby programs are executed by the Ruby interpreter (ruby), which reads the source code and executes the instructions.

Execution of Ruby Program

The following steps are involved in Ruby program execution.

Step 1: Open Notepad or any text editor and type the source code.

Ruby source code typed in a text editor (hello.rb)

Step 2: Save the above source code with the .rb extension (Filename: hello.rb).

Step 3: Execute the above program using the Ruby interpreter.

ruby hello.rb

Step 4: The result is displayed on the terminal or command prompt.

Output of ruby hello.rb in the command prompt

Structure of a Ruby Program

The structure of a Ruby program refers to the way the different components are organized to develop and execute an application.

A typical Ruby program consists of the following components:

  • Comments
  • Variables and constants
  • Input and output statements
  • Operators
  • Conditional statements
  • Looping statements
  • Methods

The general structure of a Ruby program

# Comments section

# Variable declaration
name = "Madhu"
age = 25

# Display statements
puts "Student Details"

# Conditional statement
if age >= 18
    puts "Eligible"
else
    puts "Not Eligible"
end

# Method definition
def greet
    puts "Welcome to Ruby"
end

# Method invocation
greet
Output:
Student Details
Eligible
Welcome to Ruby

Note: Ruby is a case-sensitive language. Lowercase letters and uppercase letters are distinct.

For example, the keyword end is completely different from the keyword END.