Ruby Language Metaprogramming send() method


send() is used to pass message to object. send() is an instance method of the Object class. The first argument in send() is the message that you're sending to the object - that is, the name of a method. It could be string or symbol but symbols are preferred. Then arguments those need to pass in method, those will be the remaining arguments in send().

class Hello
  def hello(*args)
    puts 'Hello ' + args.join(' ')
h =
h.send :hello, 'gentle', 'readers'   #=> "Hello gentle readers"
# h.send(:hello, 'gentle', 'readers') #=> Here :hello is method and rest are the arguments to method.

Here is the more descriptive example

class Account
  attr_accessor :name, :email, :notes, :address

  def assign_values(values)
    values.each_key do |k, v|
      # How send method would look a like
      # = value[k]
      self.send("#{k}=", values[k])

user_info = {
  name: 'Matt',
  email: '',
  address: '132 random st.',
  notes: "annoying customer"

account =
If attributes gets increase then we would messup the code
#--------- Bad way -------------- = user_info[:name]
account.address = user_info[:address] = user_info[:email]
account.notes = user_info[:notes]

# --------- Meta Programing way --------------
account.assign_values(user_info) # With single line we can assign n number of attributes

puts account.inspect

Note: send() itself is not recommended anymore. Use __send__() which has the power to call private methods, or (recommended) public_send()