Send

What is send and why does it make me feel this way?

Send (as defined by Ruby Docs) “invokes the method identified by symbol, passing it any arguments specified…When the method is identified by a string, the string is converted to a symbol.”

Check out that last line, the bit about the string being converted to a symbol.

This is super useful for when we are trying to make flexible methods, for example look at this snippet of code:

attr_accessor = :id
attr_name = "id"
value = 2
def assign(attr_name, value)
   #need to assign value to attr_name
end

I want to keep assign as flexible as possible this way if attr_name changes I don’t have to change my assign method. Attr_name is given as a string and in my method I want to call it as the method it’s defined as up in attr_accessor so that I can assign my value to it. Think about our attr_name string as a method in disguise.

attr_name is basically this white dog

This is the perfect time to use send which will convert our string to a symbol and put it to work getting assigned a value.

attr_accessor = :id
def assign(attr_name, attr_value)
self.send("#{attr_name}=", value)
end
attr_name = "id"
value = 2

Congrats you used send in a cool way!

Sources: http://ruby-doc.org/core-2.3.1/Object.html#method-i-send, http://stackoverflow.com/questions/3337285/what-does-send-do-in-ruby