Ruby 对象打印为指针

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8125946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 04:29:25  来源:igfitidea点击:

Ruby object prints out as pointer

rubyclassconstructorinitialization

提问by user1043856

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 Adder
    def initialize(my_num)
        @my_num = my_num
    end
end
y = Adder.new(12)
puts y

What am I doing wrong? Thanks

我究竟做错了什么?谢谢

回答by WarHog

When you use newmethod, you get 'reference' on newly created object. putskernel method returns some internal ruby information about this object. If you want to get any information about state your object, you can use getter method:

当您使用new方法时,您会在新创建的对象上获得“引用”。puts内核方法返回有关此对象的一些内部 ruby​​ 信息。如果要获取有关对象状态的任何信息,可以使用 getter 方法:

class Adder
  def initialize(my_num)
    @my_num = my_num
  end
  def my_num
    @my_num
  end
end
y = Adder.new(12)
puts y.my_num  # => 12

Or you can use 'attr_reader' method that define a couple of setter and getter methods behind the scene:

或者您可以使用 'attr_reader' 方法在幕后定义几个 setter 和 getter 方法:

class Adder
  attr_accessor :my_num

  def initialize(my_num)
    @my_num = my_num
  end      
end
y = Adder.new(12)
puts y.my_num  # => 12

回答by mikej

You aren't doing anything wrong. Assuming you see something like #<Adder:0xb7f9f710 @my_num=12>then in Ruby this is just the default representation of the object that you've created.

你没有做错任何事。假设您#<Adder:0xb7f9f710 @my_num=12>在 Ruby 中看到类似then 的内容,这只是您创建的对象的默认表示。

If you want to change this behaviour to be more friendly when you pass your object to putsyou can override the to_s (to string) method. e.g.

如果您想在将对象传递给puts您时将此行为更改为更友好,您可以覆盖 to_s (to string) 方法。例如

class Adder
  def initialize(my_num)
    @my_num = my_num
  end

  def to_s
    "Adder with my_num = #{@my_num}"
  end
end

then when you do puts yyou'll see Adder with my_num = 12

那么当你这样做的时候puts y你会看到Adder with my_num = 12

You can also override the inspectmethod which is what is used, for example, when the Ruby irb console prints the representation of your object e.g.

您还可以覆盖所使用的inspect方法,例如,当 Ruby irb 控制台打印对象的表示时,例如

class Adder
  def inspect
    to_s # return same representation as to_s
  end
end

then in irb:

然后在 irb:

>> y = Adder.new 12
=> Adder with my_num = 12

回答by robbrit

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

那是因为对象一个指针。在 Ruby 中,所有对象都在堆上分配,变量只是对它们的引用。

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.

它实际上是调用对象默认的to_s方法,它只是输出类名、内存位置和对象实例变量的一些信息。

回答by coreyward

Ruby does not have pointers. In your example, yis an instance of Adderwith an instance variable called @my_numwith the value of 12(which is itself a Fixnumobject).

Ruby 没有指针。在您的示例中,y是一个Adder带有实例变量的实例@my_num,其值被调用12(它本身就是一个Fixnum对象)。

The putsmethod calls the to_smethod 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_sinstance method for any class.

puts方法调用to_s您传递给它的任何参数的方法。这就是你看到的输出;也许您认为输出是指一个指针,但它只是对象的文本表示。您可以通过覆盖to_s任何类的实例方法来更改它。