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
- What is Ruby?
- Setting Up Your Environment
- Variables and Data Types
- Control Flow
- Functions (Methods)
- Arrays and Hashes
- Object-Oriented Programming
- Blocks and Iterators
- Error Handling
- 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.
1# Example: Your first Ruby program
2puts "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.
1# Example: Running a Ruby script
2ruby 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.
1# Example: Declaring variables
2age = 25
3height = 5.9
4name = "Alice"
5is_student = true
6
7puts "Name: #{name}, Age: #{age}, Height: #{height}"
Control Flow
Ruby supports if
, else
, unless
, and loops (for
, while
, until
) for controlling program flow.
1# Example: Conditional statement
2age = 18
3
4if age >= 18
5 puts "You are an adult."
6else
7 puts "You are a minor."
8end
Functions (Methods)
Functions in Ruby are called methods and are defined using the def
keyword. Methods can return values or perform actions.
1# Example: Method
2def add(a, b)
3 a + b
4end
5
6result = add(5, 10)
7puts "Result: #{result}" # 15
Arrays and Hashes
Arrays are ordered collections, while hashes are collections of key-value pairs. Both are commonly used in Ruby.
1# Example: Array
2fruits = ["Apple", "Banana", "Orange"]
3fruits.each do |fruit|
4 puts fruit
5end
6
7# Example: Hash
8person = { name: "Alice", age: 25 }
9puts "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.
1# Example: Class and Object
2class Person
3 attr_accessor :name, :age
4
5 def initialize(name, age)
6 @name = name
7 @age = age
8 end
9
10 def display
11 puts "Name: #{@name}, Age: #{@age}"
12 end
13end
14
15person = Person.new("Alice", 25)
16person.display
Blocks and Iterators
Blocks are anonymous functions that can be passed to methods. Iterators like each
and map
are commonly used with blocks.
1# Example: Block
2[1, 2, 3].each do |number|
3 puts number
4end
5
6# Example: Iterator with map
7squared_numbers = [1, 2, 3].map { |number| number ** 2 }
8puts squared_numbers.inspect # [1, 4, 9]
Error Handling
Ruby uses begin
, rescue
, and ensure
blocks for error handling, allowing you to handle exceptions gracefully.
1# Example: Error handling
2begin
3 raise "Something went wrong"
4rescue => e
5 puts "Error: #{e.message}"
6ensure
7 puts "Execution complete."
8end
File I/O
Ruby provides simple and powerful tools for reading from and writing to files.
1# Example: Reading from a file
2File.open("example.txt", "r") do |file|
3 file.each_line do |line|
4 puts line
5 end
6end
7
8# Example: Writing to a file
9File.open("output.txt", "w") do |file|
10 file.puts "Hello, Ruby!"
11end