Ruby object prints out as pointer

后端 未结 4 1456
感动是毒
感动是毒 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:34

    That's because the object is a pointer. In Ruby, all objects are allocated on the heap, and the variables are just references to them.

    When you do

    puts y
    

    It is actually calling the default to_s method of the object, which is just to output the class name, the memory location, and some info on the instance variables of the object.

提交回复
热议问题