Object#send
obj.send(symbol, *args) -> obj Returns:
Object · Updated March 13, 2026 · Core Classes methods dynamic metaprogramming core-classes
Object#send dynamically invokes a method by name at runtime. The method name can be a Symbol or String, and any additional arguments are passed to that method. This is the foundation of Ruby’s metaprogramming capabilities, enabling you to call methods whose names are determined at runtime rather than compile time.
Syntax
object.send(method_name, *arguments)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
method_name | Symbol or String | — | The name of the method to invoke |
*arguments | Object | [] | Arguments to pass to the method |
Examples
Basic usage
class Greeter
def hello(name)
"Hello, #{name}!"
end
def goodbye(name)
"Goodbye, #{name}!"
end
end
greeter = Greeter.new
greeter.send(:hello, "World")
# => "Hello, World!"
greeter.send("goodbye", "Alice")
# => "Goodbye, Alice!"
Calling private methods
class Secret
def reveal
"The password is 12345"
end
private
def hidden
"This is private"
end
end
secret = Secret.new
secret.send(:hidden)
# => "This is private"
Common Patterns
Dynamic attribute access
class Person
attr_accessor :name, :age, :email
def initialize(attrs = {})
attrs.each do |key, value|
send("#{key}=", value)
end
end
end
person = Person.new(name: "Alice", age: 30, email: "alice@example.com")
person.name # => "Alice"
person.age # => 30
Building a DSL
class Builder
def initialize(&block)
instance_eval(&block) if block_given?
end
def method_missing(name, *args)
puts "Building: #{name} with #{args.inspect}"
end
end
Builder.new do
create_user name: "Bob", email: "bob@example.com"
send_email to: "bob@example.com"
end
# Building: create_user with [{:name=>"Bob", :email=>"bob@example.com"}]
# Building: send_email with [{:to=>"bob@example.com"}]
Errors
- NoMethodError: Raised if the method does not exist and is not handled by method_missing
- ArgumentError: Raised if the wrong number of arguments are passed