Blocks

Build a big expertise of Ruby –
One small block of knowledge at a time.


Flexible Hash Arguments in Ruby

The double splat operator (**) allows methods to accept an arbitrary number of keyword arguments as a hash. Perfect for creating flexible APIs and handling optional parameters.

def greet(name:, age:, **options)
  puts "Hello #{name}, you are #{age} years old"
  puts "Additional info: #{options}" if options.any?
end

# Call with extra arguments
greet(name: "John", age: 30, city: "New York", occupation: "Developer")

Use it when you need to pass through additional options or create flexible method signatures. Great for API wrappers, configuration objects, and method delegation.