I'm reading a document that talks about a method having a receiver. What's a receiver?
In Ruby (and other languages that take inspiration from SmallTalk) objects are thought of as sending and receiving 'messages'. In Ruby, Object, the base class of everything, has a send method: Object.
inject(:+) is not Symbol#to_proc, :+ has no special meaning in the ruby language - it's just a symbol.
Ruby doesn't really have functions. Rather, it has two slightly different concepts - methods and Procs (which are, as we have seen, simply what other languages call function objects, or functors). Both are blocks of code - methods are bound to Objects, and Procs are bound to the local variables in scope.
Object: That's just any piece of data. Like the number 3 or the string 'hello'. Class: Ruby separates everything into classes. Like integers, floats and strings. Method: These are the things that you can do with an object.
In Ruby (and other languages that take inspiration from SmallTalk) objects are thought of as sending and receiving 'messages'.
In Ruby, Object, the base class of everything, has a send method: Object.send For example:
class Klass def hello "Hello!" end end k = Klass.new k.send :hello #=> "Hello!" k.hello #=> "Hello!"
In both of these cases k is the receiver of the 'hello' message.
In the original Smalltalk terminology, methods on "objects" were instead refered to as messages to objects (i.e. you didn't call a method on object foo, you sent object foo a message). So foo.blah is sending the "blah" message, which the "foo" object is receiving; "foo" is the receiver of "blah".
the object before the .
think of calling a method x.y as saying "send instruction y to object x".
it's the smalltalk way of thinking, it will serve you well as you get to some of Ruby's more advanced features.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With