defining_methods
📝 Defining and Calling Methods
In Ruby, methods are defined using the def
keyword followed by a name and optional parameters. They help you encapsulate logic, making your code modular and reusable. You call a method by simply using its name and passing required arguments.
# Define a method
def greet(name)
"Hello, #{name}!"
end
# Call the method
greeting = greet("Alice")
puts greeting # => "Hello, Alice!"