Ruby is a dynamic, open-source programming language known for its simplicity, productivity, and elegance. Developed in the mid-1990s by Yukihiro Matsumoto, Ruby has gained popularity for web development, scripting, and automation. This guide introduces the basics of Ruby programming to help beginners get started.
Ruby Basics: A Comprehensive Guide for Beginners. |
Ruby is an interpreted, high-level language designed with an emphasis on simplicity and productivity. Its syntax is easy to read and write, making it beginner-friendly. Ruby is often associated with the Ruby on Rails framework, which is widely used for web development.
To start coding in Ruby, you need:
Example - Hello World:
puts 'Hello, World!'
Explanation:
puts
: Outputs text followed by a newline.Variables and Types:
name = "Alice" # String
age = 25 # Integer
height = 5.8 # Float
is_student = true # Boolean
colors = ["red", "blue", "green"] # Array
Ruby variables are dynamically typed, so you don't need to declare data types explicitly.
Constants:
PI = 3.14
Reading Input:
puts 'Enter your name:'
name = gets.chomp # Removes trailing newline
puts "Hello, #{name}!"
Conditional Statements:
age = 18
if age >= 18
puts 'Adult'
elsif age > 12
puts 'Teenager'
else
puts 'Child'
end
Loops:
# For Loop
for i in 1..5
puts i
end
# While Loop
count = 0
while count < 5
puts count
count += 1
end
# Times Loop
5.times do |i|
puts i
end
Arrays:
fruits = ["apple", "banana", "cherry"]
puts fruits[0] # Access first element
fruits.push("grape") # Add an element
Hashes (Dictionaries):
person = {name: "John", age: 30, city: "New York"}
puts person[:name] # Access value by key
Defining Methods:
def greet(name)
"Hello, #{name}!"
end
puts greet("Alice")
Blocks:
3.times do |i|
puts "Iteration: #{i}"
end
Lambda Functions:
square = ->(x) { x * x }
puts square.call(5) # Output: 25
Ruby is a fully object-oriented language. Everything in Ruby is an object.
Classes and Objects:
class Person
attr_accessor :name, :age
def initialize(name, age)
@name = name
@age = age
end
def greet
puts "Hello, my name is #{@name} and I am #{@age} years old."
end
end
p = Person.new("Alice", 25)
p.greet
Modules allow grouping of related methods and can be mixed into classes.
Example:
module Greetings
def say_hello
puts "Hello!"
end
end
class User
include Greetings
end
u = User.new
u.say_hello
Writing to a File:
File.open('output.txt', 'w') do |file|
file.puts 'Ruby File Handling'
end
Reading from a File:
File.open('output.txt', 'r') do |file|
puts file.read
end
Example:
begin
result = 10 / 0
rescue ZeroDivisionError => e
puts "Error: #{e.message}"
ensure
puts "Execution completed."
end
Ruby is a versatile and beginner-friendly programming language that combines simplicity with power. Its intuitive syntax, dynamic typing, and object-oriented features make it ideal for web development, scripting, and data processing. Whether you're building small scripts or large applications, Ruby provides the tools you need to write clean and efficient code.