THe inner workings of Ruby Message Passing : Lesson 10 of My Self-Inflicted Crash Course into Ruby

Calling an object method in Ruby behind the scenes is actually a message to another object or Message Passing. These can more easily be understood by an example, and for that reason lets look at a basic multiplication script.

3 x 4

As you are aware everything in Ruby is an object, including an integer. Therefor the above operation is really a call to the x method of the integer object.

So far this isn't very surprising as you are calling the multiplication method of the integer object, however you can call the same method by using the send function passing the method name and parameters. This is called Message Passing.

3.send "x" 4

You may wonder why does it matter. The answer simply put is that you can call any method of an object regardless of its visibility by using the send method. If you want to call a private method of an object that you normally wouldn't be able to call then by using send you can do so. Ruby has actually tried to restrict this functionality but has been forced to retain it and instead also now provides a public_send method in version 1.9 for those cases you don't want to access private methods.

Seriously what is the point to having Method Visibility if the underlying fundamentals of the language itself allow developers to bypass them. A ball was definitely dropped somewhere during the creation of the Ruby language.

// Ruby //

Comments & Questions

Add Your Comment