gpt4 book ai didi

ruby - 用 defined? 缩短 Ruby 代码?

转载 作者:太空宇宙 更新时间:2023-11-03 17:12:14 24 4
gpt4 key购买 nike

在 Ruby 中是否有更短的编写下一段代码的方法:

if defined?($servlet_context) then true
else false
end

最佳答案

defined? operator evaluates to a string or nil :

The defined? operator returns nil if its argument (which can be an arbitrary expression) is not defined, otherwise it returns a description of that argument.

因此,如果您只是想要真实的东西,那么 defined?($servlet_context) 就可以了。然而,如果你绝对必须有一个 bool 值,那么你可以使用双爆炸:

!!defined?($servlet_context)

关于ruby - 用 defined? 缩短 Ruby 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9529785/

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