The Send Method: What it does and when to use it

Brian Patrick Tagalog/Unsplash

First, let’s go over the need-to-know essentials. When you’re calling n method on an object using dot (.) notation, like in the example below, you’re essentially passing a message to it.

* The string is the object.
* The dot is the method in which we’re sending the object a message or command.
* Downcase or the argument is the message.

We can accomplish the same with the send method.

.send allows you to send a method call this way:


When to use the send method:

Seeing it in an example makes it easier to comprehend when you should use it.

In the Student Scraper lab in the OO Ruby section, we had to define an initialize method for our Student class that a) takes in an argument of a hash and b) uses metaprogramming to assign the newly created student attributes and values in accordance with the key/value pairs of the hash”

Here, the send method is essentially taking attributes-and-value pairs in the existing student hash and churning out instance variables and value assignments.

Send method uses in Ruby:
1. Allows you to assign attributes
2. Allows you to call on methods by name with arguments
3. Allows you to call on methods without explicitly writing each individual method name every time

* I found this article on metaprogramming in ruby provided really helpful for understand what the send method/command does and when to use it.
* Found this video very helpful in really understanding the send method.
* And this forum covers most of the send use cases



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store