Ruby:叫什么方法?

发布于 2024-08-19 01:48:15 字数 442 浏览 3 评论 0原文

假设我有一个 MyClass 的对象 x。当我 puts x 时,会调用什么方法?我需要用我自己的覆盖它。

我以为它是 .inspect,但不知何故重写的 inspect 没有被调用。

例如,我有一个类 Sum

class Sum
  initiazlie a, b
     @x = a + b
  end
end

我想像这样访问结果:

s = Sum.new(3,4)
puts s         #=>    7   How do I do this?
puts 10 + s    #=>   17   or even this...?

Assume, I have an object x of MyClass. What method is called, when I do puts x? I need to override it with my own one.

I thought it was .inspect, but somehow overridden inspect isn't being called.

For example, I have a class Sum:

class Sum
  initiazlie a, b
     @x = a + b
  end
end

And I wanna access the result like this:

s = Sum.new(3,4)
puts s         #=>    7   How do I do this?
puts 10 + s    #=>   17   or even this...?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

z祗昰~ 2024-08-26 01:48:15

它调用:to_s

class Sum
  def to_s
    "foobar"
  end
end

puts Sum.new #=> 'foobar'

或者,如果您愿意,您可以从 to_s 调用 inspect,以便获得对象的一致字符串表示形式。

class Sum
  def to_s
    inspect
  end
end

It calls: to_s

class Sum
  def to_s
    "foobar"
  end
end

puts Sum.new #=> 'foobar'

Or if you want, you can just call inspect from to_s, so that you have a consistent string representation of your object.

class Sum
  def to_s
    inspect
  end
end
苏大泽ㄣ 2024-08-26 01:48:15

首先,您的 Sum 类无效。定义应该是。

class Sum
  def initialize(a, b)
     @x = a + b
  end
end

默认情况下,为获取人类可读表示而调用的方法是检查。在 irb 中尝试此操作

$ s = Sum.new(3, 4)
# => #<Sum:0x10041e7a8 @x=7>
$ s.inspect
# => "#<Sum:0x10041e7a8 @x=7>"

,但在您的情况下,您使用强制字符串转换的 puts 方法。因此,首先使用 to_s 方法将 Sum 对象转换为字符串。

$ s = Sum.new(3, 4)
# => #<Sum:0x10041e7a8 @x=7>
$ puts s
# => #<Sum:0x10041e7a8>
$ puts s.to_s
# => #<Sum:0x10041e7a8>

另请注意,您的最后一个示例属于第三种情况。因为您对 Fixnum + 另一个对象求和,所以结果预计是一个 Fixnum,并且调用的方法是 to_s,但是是在 Fixnum 类中定义的方法。

为了使用 Sum 类中的项,您需要切换 sum 中的项目并在对象中定义 +

class Sum
  def initialize(a, b)
     @x = a + b
  end
  def +(other)
     @x + other
  end
  def to_s
    @x.to_s
  end
end

s = Sum.new(3, 4)
s + 10
puts s
# => 17

First, your Sum class is invalid. The definition should be.

class Sum
  def initialize(a, b)
     @x = a + b
  end
end

By default, the method called to get a human readable representation is inspect. Try this in irb

$ s = Sum.new(3, 4)
# => #<Sum:0x10041e7a8 @x=7>
$ s.inspect
# => "#<Sum:0x10041e7a8 @x=7>"

But in your case, you use the puts method which forces a string conversion. For this reason, the Sum object is first converted to string using the to_s method.

$ s = Sum.new(3, 4)
# => #<Sum:0x10041e7a8 @x=7>
$ puts s
# => #<Sum:0x10041e7a8>
$ puts s.to_s
# => #<Sum:0x10041e7a8>

Also note your last example fall into a third case. Because you sum a Fixnum + an other object, the result is expected to be a Fixnum and the method called is to_s but the one defined in the Fixnum class.

In order to use the one in your Sum class, you need to switch the items in the sum and define the + in your object.

class Sum
  def initialize(a, b)
     @x = a + b
  end
  def +(other)
     @x + other
  end
  def to_s
    @x.to_s
  end
end

s = Sum.new(3, 4)
s + 10
puts s
# => 17
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文