gpt4 book ai didi

Ruby-初学者遇到未知语法

转载 作者:数据小太阳 更新时间:2023-10-29 08:36:37 25 4
gpt4 key购买 nike

在尝试学习 Ruby 时,我遇到了这种语法......任何人都可以解释一下这是什么意思吗?

a = nil if b.nonzero?

nonzero? : Returns self if num is not zero, nil otherwise.

因此不返回 bool 值

最佳答案

ruby 中的值分为真值和假值。也就是说,如果一个值不是 nil 或 false,那么它就是 true。因此,如果您有一个返回 1 的函数,您可以在 bool 表达式中使用它 some_function && true 将解析为 true。

同样,如果它返回 nilsome_function && true 将返回 false。

这里有详细深入的解释:https://gist.github.com/jfarmer/2647362

关于Ruby-初学者遇到未知语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34355548/

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