Ruby object prints out as pointer

后端 未结 4 1458
感动是毒
感动是毒 2021-02-02 06:50

I\'m trying to create a class, which has a constructor that takes a single argument. When I create a new instance of the object, it returns a pointer.

class Adde         


        
4条回答
  •  一个人的身影
    2021-02-02 07:35

    Ruby does not have pointers. In your example, y is an instance of Adder with an instance variable called @my_num with the value of 12 (which is itself a Fixnum object).

    The puts method calls the to_s method of whatever arguments you pass it. That's what you see output; perhaps you think that output refers to a pointer, but it's just a textual representation of the object. You can change it by overriding the to_s instance method for any class.

提交回复
热议问题