In Ruby what does the “receiver” refer to?

前端 未结 3 438
执笔经年
执笔经年 2021-01-01 13:30

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

相关标签:
3条回答
  • 2021-01-01 14:23

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

    0 讨论(0)
  • 2021-01-01 14:33

    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.

    0 讨论(0)
  • 2021-01-01 14:34

    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.

    0 讨论(0)
提交回复
热议问题