views:

202

answers:

3

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

+1  A: 

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.

chillitom
+1  A: 

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

Adam Wright
+6  A: 

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.

Cameron Pope
Is "k.send :hello" actually a syntactically valid way of calling "k.hello" in Ruby?
lorz
You say k is the receiver. So why do we say "k.send :hello" instead of "k.receive :hello"? It *sounds* like k is the sender rather than the receiver.
lorz
Because you're sending TO k, and not receiving TO k. That latter option makes little sense. ;)
The Wicked Flea