Ruby method like `self` that refers to instance

前端 未结 5 363
迷失自我
迷失自我 2021-02-02 13:50

Is there a method in Ruby that refers to the current instance of a class, in the way that self refers to the class itself?

5条回答
  •  臣服心动
    2021-02-02 14:11

    The self reference is always available, and the object it points to depends on the context.

    class Example
    
      self  # refers to the Example class object
    
      def instance_method
        self  # refers to the receiver of the :instance_method message
      end
    
    end
    

提交回复
热议问题