Ruby Methods
Reusable blocks of code that perform specific tasks
🔧 What are Ruby Methods?
Methods are reusable blocks of code that perform specific tasks. They help organize your code, make it cleaner, and avoid repetition. Define once, use many times!
# Simple method definition
def greet
puts "Hello, World!"
end
# Call the method
greet
Output:
Hello, World!
Key Method Concepts
Definition
Use 'def' keyword to create methods
def say_hello
puts "Hi!"
end
Calling
Execute methods by using their name
say_hello
# Output: Hi!
Naming
Use lowercase with underscores
def calculate_total
# code here
end
Return
Methods automatically return last value
def add
2 + 3
end
🔹 Basic Method Structure
Methods in Ruby follow a simple structure. They start with 'def', contain code, and end with 'end'. This makes your code organized and reusable for any task you need to repeat.
# Method structure
def method_name
# Code goes here
puts "This is inside the method"
end
# Call the method
method_name
Output:
This is inside the method
🔹 Methods with Parameters
Parameters allow methods to accept input values, making them flexible and dynamic. You can pass different values each time you call the method, enabling it to perform the same task with different data.
# Method with one parameter
def greet_person(name)
puts "Hello, #{name}!"
end
greet_person("Alice")
greet_person("Bob")
# Method with multiple parameters
def introduce(name, age)
puts "I'm #{name} and I'm #{age} years old"
end
introduce("Charlie", 25)
Output:
Hello, Alice!
Hello, Bob!
I'm Charlie and I'm 25 years old
🔹 Methods with Default Parameters
Default parameters provide fallback values when no argument is passed. This makes methods more flexible, allowing them to work with or without specific inputs while maintaining expected behavior.
# Default parameter value
def greet(name = "Friend")
puts "Hello, #{name}!"
end
greet("Sarah") # Uses provided value
greet # Uses default value
# Multiple defaults
def create_user(name, role = "user", active = true)
puts "#{name} is a #{role}, Active: #{active}"
end
create_user("John")
create_user("Admin", "admin", false)
Output:
Hello, Sarah!
Hello, Friend!
John is a user, Active: true
Admin is a admin, Active: false
🔹 Return Values
Ruby methods automatically return the last evaluated expression. You can also use the 'return' keyword explicitly to exit early or make your intention clear, though it's often optional in Ruby.
# Implicit return (last line)
def add(a, b)
a + b
end
result = add(5, 3)
puts result
# Explicit return
def multiply(a, b)
return a * b
end
puts multiply(4, 6)
# Early return
def check_age(age)
return "Too young" if age < 18
"Welcome!"
end
puts check_age(15)
puts check_age(25)
Output:
8
24
Too young
Welcome!
🔹 Practical Examples
Real-world method examples demonstrate how to solve common programming tasks. These patterns show you how to structure methods for calculations, validations, and data processing in your Ruby applications.
# Calculator method
def calculate_area(length, width)
length * width
end
puts "Area: #{calculate_area(5, 10)}"
# String manipulation
def format_name(first, last)
"#{first.capitalize} #{last.capitalize}"
end
puts format_name("john", "doe")
# Conditional method
def is_even?(number)
number % 2 == 0
end
puts is_even?(4)
puts is_even?(7)
Output:
Area: 50
John Doe
true
false