'What does send() do in Ruby?

Can someone please tell me what

send("#{Model.find...}")

is and does?



Solution 1:[1]

send is a ruby (without rails) method allowing to invoke another method by name.

From documentation

   class Klass
     def hello(*args)
       "Hello " + args.join(' ')
     end
   end
   k = Klass.new
   k.send :hello, "gentle", "readers"   #=> "Hello gentle readers"

http://corelib.rubyonrails.org/classes/Object.html#M001077

Solution 2:[2]

One of the most useful feature I think with .send method is that it can dynamically call on method. This can save you a lot of typing. One of the most popular use of .send method is to assign attributes dynamically. For example:

class Car
  attr_accessor :make, :model, :year
end  

To assign attributes regularly one would need to

c = Car.new
c.make="Honda"
c.model="CRV"
c.year="2014"

Or using .send method:

c.send("make=", "Honda")
c.send("model=", "CRV")
c.send("year=","2014")

But it can all be replaced with the following:

Assuming your Rails app needs to assign attributes to your car class from user input, you can do

c = Car.new()
params.each do |key, value|
  c.send("#{key}=", value)
end

Solution 3:[3]

Another example, similar to Antonio Jha's https://stackoverflow.com/a/26193804/1897857

is if you need to read attributes on an object.

For example, if you have an array of strings, if you try to iterate through them and call them on your object, it won't work.

atts = ['name', 'description']
@project = Project.first
atts.each do |a|
  puts @project.a
end
# => NoMethodError: undefined method `a'

However, you can send the strings to the object:

atts = ['name', 'description']
@project = Project.first
atts.each do |a|
  puts @project.send(a)
end
# => Vandalay Project
# => A very important project

Solution 4:[4]

What does send do?

send is another way of "calling a method". Example:

o = Object.new
o.to_s # => "#<Object:0x00005614d7a24fa3>"
# is equivalent to:
o.send(:to_s) # => "#<Object:0x00005614d7a24fa3>"

Send lives in the Object class.

What is the benefit of this?

The benefit of this approach is that you can pass in the method you want to call as a parameter. Here is a simple example:

def dynamically_call_a_method(method_name)
    o = Object.new
    o.send method_name
end
dynamically_call_a_method(:to_s) # => "#<Object:0x00005614d7a24fa3>"

You can pass in the method you want to be called. In this case we passed in :to_s. This can be very handy when doing ruby metaprogramming, because this allows us to call different methods according to our different requirements.

Solution 5:[5]

Send can also be used as a way of showing how everything in Ruby is an object

1.send(:+, 1)  ## -> 2
3.send(:*, 2)  ## -> 6

Solution 6:[6]

Another use case for views:

    <%= link_to 
    send("first_part_of_path_#{some_dynamic_parameters}_end_path", 
    attr1, attr2), ....
    %>

Allow . you to write scalable view who work with all kind of objects with:

    render 'your_view_path', object: "my_object"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nikita Rybak
Solution 2 the12
Solution 3 Community
Solution 4
Solution 5 DuDa
Solution 6 mahatmanich