Call Dynamic Method in Ruby

| Comments

I love Ruby’s syntax for method calls. The syntax for calling dynamic methods is pretty nice, and it’s easily mastered.

Ruby

send method

The send method is one of the best and most basic ways to call a method dynamically on your object. Let’s say we have an object with multiple methods like this:

1
2
3
4
5
6
7
class Fezzik
  def show_sportsmanship
  end

  def be_colossus_for(person_1, person_2=nil, person_3=nil)
  end
end

The method you want to call might be represented as a string or a symbol. Your method call might look like this:

1
2
3
Fezzik.new.send(:show_sportsmanship)
# or
Fezzik.new.send("show_sportsmanship")

Calling send with parameters

Now, some of your methods might have parameters. How are those called? Simply pass the argument values into the send call as the final argument, which is a variable length argument:

1
2
just_one_person = Person.new
Fezzik.new.send(:be_colossus_for, just_one_person)

You can also pass the parameters as a splat array, such as this:

1
2
people = [Person.new, Person.new, Person.new]
Fezzik.new.send(:be_colossus_for, *people)

In Ruby, the splat operator destructures the array into parameters, so the above code essentially becomes:

1
2
people = [Person.new, Person.new, Person.new]
Fezzik.new.send(:be_colossus_for, people[0], people[1], people[2])

So, send turns out to be pretty easy to deal with and really useful. Use it, and be happy.

Comments