Introduction to Programming in Ruby

Ruby is a dynamic, object-oriented programming language designed for simplicity and productivity. It has an elegant syntax that is natural to read and easy to write, making it a favorite among developers for web development, scripting, and automation. Ruby is the language behind the popular web framework Ruby on Rails.

This guide will introduce you to the fundamentals of Ruby programming, helping you write clean, expressive, and efficient code.


Table of Contents

  1. What is Ruby?
  2. Setting Up Your Environment
  3. Variables and Data Types
  4. Control Flow
  5. Functions (Methods)
  6. Arrays and Hashes
  7. Object-Oriented Programming
  8. Blocks and Iterators
  9. Error Handling
  10. File I/O

What is Ruby?

Ruby is a high-level, interpreted programming language that emphasizes simplicity and productivity. It is often used for web development, scripting, and building automation tools.

# Example: Your first Ruby program
puts "Hello, Ruby Programming!"

Setting Up Your Environment

To write and run Ruby programs, you need to install Ruby on your system. Follow the instructions on the official Ruby website.

# Example: Running a Ruby script
ruby main.rb

Variables and Data Types

Ruby is dynamically typed, meaning you don’t need to declare variable types explicitly. Common data types include integers, floats, strings, and booleans.

# Example: Declaring variables
age = 25
height = 5.9
name = "Alice"
is_student = true

puts "Name: #{name}, Age: #{age}, Height: #{height}"

Control Flow

Ruby supports if, else, unless, and loops (for, while, until) for controlling program flow.

# Example: Conditional statement
age = 18

if age >= 18
  puts "You are an adult."
else
  puts "You are a minor."
end

Functions (Methods)

Functions in Ruby are called methods and are defined using the def keyword. Methods can return values or perform actions.

# Example: Method
def add(a, b)
  a + b
end

result = add(5, 10)
puts "Result: #{result}" # 15

Arrays and Hashes

Arrays are ordered collections, while hashes are collections of key-value pairs. Both are commonly used in Ruby.

# Example: Array
fruits = ["Apple", "Banana", "Orange"]
fruits.each do |fruit|
  puts fruit
end

# Example: Hash
person = { name: "Alice", age: 25 }
puts "Name: #{person[:name]}, Age: #{person[:age]}"

Object-Oriented Programming

Ruby is a pure object-oriented language, meaning everything in Ruby is an object. Classes and objects are fundamental to Ruby programming.

# Example: Class and Object
class Person
  attr_accessor :name, :age

  def initialize(name, age)
    @name = name
    @age = age
  end

  def display
    puts "Name: #{@name}, Age: #{@age}"
  end
end

person = Person.new("Alice", 25)
person.display

Blocks and Iterators

Blocks are anonymous functions that can be passed to methods. Iterators like each and map are commonly used with blocks.

# Example: Block
[1, 2, 3].each do |number|
  puts number
end

# Example: Iterator with map
squared_numbers = [1, 2, 3].map { |number| number ** 2 }
puts squared_numbers.inspect # [1, 4, 9]

Error Handling

Ruby uses begin, rescue, and ensure blocks for error handling, allowing you to handle exceptions gracefully.

# Example: Error handling
begin
  raise "Something went wrong"
rescue => e
  puts "Error: #{e.message}"
ensure
  puts "Execution complete."
end

File I/O

Ruby provides simple and powerful tools for reading from and writing to files.

# Example: Reading from a file
File.open("example.txt", "r") do |file|
  file.each_line do |line|
    puts line
  end
end

# Example: Writing to a file
File.open("output.txt", "w") do |file|
  file.puts "Hello, Ruby!"
end