[英] to_s vs. to_str (and to_i/to_a/to_h vs. to_int/to_ary/to_hash) in Ruby
我正在学习Ruby,我看到了一些让我有点困惑的方法,尤其是to_s
对to_str
(类似地,to_i
/to_int
、to_a
/to_ary
和to_h
/to_hash
).我所读到的解释是,较短的形式(例如to_s
)用于显式转换,而较长的形式用于隐式转换.
我真的不明白to_str
是怎么用的.除了字符串之外,还有什么东西能定义to_str
吗?你能给出这种方法的实际应用吗?