Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Ruby what does the "receiver" refer to?

I'm reading a document that talks about a method having a receiver. What's a receiver?

like image 347
lorz Avatar asked May 27 '09 15:05

lorz


People also ask

What is receiver in Ruby?

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.

What does :+ mean in Ruby?

inject(:+) is not Symbol#to_proc, :+ has no special meaning in the ruby language - it's just a symbol.

What are functions called in Ruby?

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.

What does object mean in Ruby?

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.


3 Answers

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.

like image 154
Cameron Pope Avatar answered Sep 21 '22 02:09

Cameron Pope


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".

like image 28
Adam Wright Avatar answered Sep 20 '22 02:09

Adam Wright


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.

like image 31
chillitom Avatar answered Sep 17 '22 02:09

chillitom