gpt4 book ai didi

Ruby:为什么 nil.to_s 返回 "",但 nil.inspect 返回 "nil"

转载 作者:数据小太阳 更新时间:2023-10-29 06:51:33 28 4
gpt4 key购买 nike

为什么 nil.to_s 返回 "",而 nil.inspect 返回 "nil"(当显然 .inspect 使用 .to_s 方法)

最佳答案

"inspect" method of the Object class应该返回对象的人类可读版本。

nil 的人类可读版本是“nil”而不是“”(这是字符串"" 的人类可读版本)。
这就是为什么 nil.inspect 应该返回“nil”,而 nil.to_s 返回“”以便 "a string"+ nil 返回“a string” 正如通常所期望的那样(例如在其他语言中)。

关于Ruby:为什么 nil.to_s 返回 "",但 nil.inspect 返回 "nil",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12395286/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com