简体   繁体   English

ruby Array.inspect 与 Array[element].to_s

[英]ruby Array.inspect vs. Array[element].to_s

I'm working with an array, which we'll call books<\/code> , of complex objects, which we'll call Book<\/code> .我正在处理一个复杂对象的数组,我们称之为books<\/code> ,我们称之为Book<\/code> 。 The problem is when I call puts "#{books.inspect}"<\/code> , ruby outputs a stream of binary (unreadable utf8 characters).问题是当我调用puts "#{books.inspect}"<\/code>时,ruby 输出二进制流(不可读的 utf8 字符)。 However, when I call puts #{books[0].to_str}"<\/code> , I get a brief, pretty output that describes the book<\/code> in question. Not sure if it is relevant, but Book<\/code> is a subclass (we can call it's parent class Item<\/code> ), and books.length=1<\/code>但是,当我调用puts #{books[0].to_str}"<\/code>时,我会得到一个简短、漂亮的输出来描述所讨论的book<\/code> 。不确定它是否相关,但Book<\/code>是一个子类(我们可以称它为父类Item<\/code> ),并且books.length=1<\/code>

Ruby<\/a> implies that .to_s<\/code> and .inspect<\/code> are synonymous, but they are clearly providing different results in practice. Ruby<\/a>暗示.to_s<\/code>和.inspect<\/code>是同义词,但它们在实践中显然提供了不同的结果。 Does anyone know why this is happening and can you provide a suggestion for how to get the nice output I want out of the whole books collection?有谁知道为什么会发生这种情况,您能否就如何从整个图书收藏中获得我想要的出色输出提供建议?

Misc info:杂项信息:

[chk ~ ]$ ruby -v
ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-linux]
class Myclass
    def to_s
        'my string representation'
    end
    def inspect
        'my inspection'
    end
end
a= [Myclass.new]
p a
puts a

outputs ::
[my inspection]
my string representation

books.inspect<\/code> and books[0].to_s<\/code> are absolutely NOT the same. books.inspect<\/code>和books[0].to_s<\/code>绝对不一样。 The first is a call to inspect<\/code> method of book<\/code> object, which is an array.第一个是调用book<\/code>对象的inspect<\/code>方法,它是一个数组。 The second is a call to to_s<\/code> method of books[0]<\/code> object, whatever it is that is contained inside the array.第二个是对books[0]<\/code>对象的to_s<\/code>方法的调用,无论它包含在数组中的是什么。 As you didn't specify what exactly is books[0]<\/code> , I'm afraid I can't tell anything more.由于您没有具体说明books[0]<\/code>的确切含义,因此恐怕我不能再多说什么了。

"

books<\/code> is an array so calling books.inspect<\/code> is calling Array#inspect<\/code> . books<\/code>是一个数组,因此调用books.inspect<\/code>就是调用Array#inspect<\/code> 。 That method works by calling .inspect<\/code> on the elements, so in this case Book#inspect<\/code> .该方法通过在元素上调用.inspect<\/code>来工作,因此在本例中为Book#inspect<\/code> 。

Doing string-interpolation "Here's some info #{value}"<\/code> calls .to_s<\/code> on the value<\/code> object whatever its class may be.执行字符串插值"Here's some info #{value}"<\/code>会在value<\/code>对象上调用.to_s<\/code> ,无论其类是什么。

Putting these together as an example "Books #{books}"<\/code> would call Array#to_s<\/code> (which is an alias for Array#inspect<\/code> ) that calls Book#inspect<\/code> to produce the output string.将这些放在一起作为示例"Books #{books}"<\/code>将调用Array#to_s<\/code> (这是Array#inspect<\/code>的别名),它调用Book#inspect<\/code>来生成输出字符串。

When you want things to work consistently, you should define inspect<\/code> and make to_s<\/code> an alias of inspect<\/code> as is done in the Array<\/code> class as an example.当您希望事情始终如一地工作时,您应该定义inspect<\/code>并将to_s<\/code>设为inspect<\/code>的别名,就像在Array<\/code>类中所做的那样。 eg例如

class MyClass
  def inspect
    'my string representation'
  end
  alias_method :to_s, :inspect
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM