One of the most useful functions that I think using the .send method is that it can dynamically call a method. This can save you a lot of typing. One of the most popular ways to use .send is to dynamically assign attributes. For example:
class Car attr_accessor :make, :model, :year end
To assign attributes regularly, you need to
c = Car.new c.make="Honda" c.model="CRV" c.year="2014"
Or using the .send method:
c.send("make=", "Honda") c.send("model=", "CRV") c.send("year=","2014")
But all this can be replaced by the following:
Assuming your Rails application needs to assign attributes to your car class with user input, you can do
c = Car.new() params.each do |key, value| c.send("#{key}=", value) end
Antonio Jha 04 Oct '14 at 2:31 a.m. 2014-10-04 14:31
source share