PHP中文网2017-04-24 09:11:51
First question:
to_s should be a widely existing method, and to_str should be String or a method that only a few classes have. Ruby does have many methods, https://ruby-china.org/topics/25311.
Second question:
The return type of inspect is string, which returns a human-readable representation of the current object. nil.inpsect returns "nil"
因为他就是返回 "nil"
, just look at the NilClass#inspect documentation.
怪我咯2017-04-24 09:11:51
A problem needs to be pointed out here, that is, nil
跟其他语言中的null
是不一样的,nil
in Ruby is an object (not a class, it is an object of the NilClass class). This object is used in Ruby to represent without anything, but it is an object in itself .