Ruby Tutorial
Introduction to Ruby
Ruby is a dynamic, open-source programming language with a focus on simplicity and productivity. It has an elegant syntax that is natural to read and easy to write. Ruby was created by Yukihiro "Matz" Matsumoto in the mid-1990s in Japan.
Installing Ruby
To install Ruby, you can use a version manager like RVM (Ruby Version Manager) or asdf. Here we will use RVM.
Open your terminal and run the following commands:
gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 \curl -sSL https://get.rvm.io | bash -s stable --ruby
Once the installation is complete, you can verify the installation by running:
ruby -v
Basic Syntax
Let's start with the basic syntax of Ruby. Here's a simple "Hello, World!" program:
puts "Hello, World!"
Save the above code in a file named hello.rb and run it using the following command:
ruby hello.rb
The output should be:
Variables and Data Types
Ruby supports various data types including integers, floats, strings, arrays, and hashes. Here's an example demonstrating the use of different data types:
# Integer
age = 25
# Float
height = 5.9
# String
name = "Alice"
# Array
fruits = ["apple", "banana", "cherry"]
# Hash
person = {name: "Alice", age: 25, height: 5.9}
puts age
puts height
puts name
puts fruits
puts person
Control Structures
Ruby supports various control structures such as if-else, loops, and case statements. Here's an example:
# if-else
age = 18
if age >= 18
puts "You are an adult."
else
puts "You are a minor."
end
# while loop
i = 1
while i <= 5
puts i
i += 1
end
# for loop
for i in 1..5
puts i
end
# case statement
grade = "A"
case grade
when "A"
puts "Excellent!"
when "B"
puts "Good!"
when "C"
puts "Fair!"
else
puts "Poor!"
end
Methods
Methods in Ruby are a way to encapsulate reusable code. Here's how you define and call a method:
# Method definition
def greet(name)
puts "Hello, #{name}!"
end
# Method call
greet("Alice")
Classes and Objects
Ruby is an object-oriented language. You can define classes and create objects in Ruby. Here's an example:
class Person
def initialize(name, age)
@name = name
@age = age
end
def display_details
puts "Name: #{@name}, Age: #{@age}"
end
end
# Create an object
person1 = Person.new("Alice", 25)
person1.display_details
Modules
Modules in Ruby are a way to group related methods. They can be mixed into classes using the include keyword. Here's an example:
module Greet
def say_hello
puts "Hello!"
end
end
class Person
include Greet
end
person = Person.new
person.say_hello
File Handling
Ruby provides various methods to handle files. Here's how you can read from and write to a file:
# Writing to a file
File.open("example.txt", "w") do |file|
file.puts "Hello, World!"
end
# Reading from a file
File.open("example.txt", "r") do |file|
file.each_line do |line|
puts line
end
end
Exception Handling
Ruby provides a way to handle exceptions using the begin-rescue block. Here's an example:
begin
# Code that may raise an exception
result = 10 / 0
rescue ZeroDivisionError => e
puts "Error: #{e.message}"
end
Conclusion
In this tutorial, we've covered the basics of Ruby, including its syntax, variables, control structures, methods, classes, modules, file handling, and exception handling. Ruby is a powerful and flexible language with a rich ecosystem. To learn more, you can explore the official Ruby documentation.
