Call Dynamic Method in Ruby
I love Ruby’s syntax for method calls. The syntax for calling dynamic methods is pretty nice, and it’s easily mastered.
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:
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:
Fezzik.new.send(:show_sportsmanship) # or Fezzik.new.send("show_sportsmanship")
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:
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:
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:
people = [Person.new, Person.new, Person.new] Fezzik.new.send(:be_colossus_for, people, people, people)
send turns out to be pretty easy to deal with and really useful. Use it, and be happy.