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.
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 .
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.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 .