gpt4 book ai didi

ruby-on-rails - 如何测试变量是否已定义?

转载 作者:数据小太阳 更新时间:2023-10-29 07:46:30 24 4
gpt4 key购买 nike

这是我想做的伪代码:

if @current_user is defined then puts @current_user.name

最佳答案

使用运算符 defined?那么。

x = 10
defined? x # => "local-variable"
defined? y # => nil
@x = 10
defined? @x # => "instance-variable"
!!defined? x # => true
!!defined? y # => false

编写如下代码:

puts @current_user.name if !!defined?(@current_user)

关于ruby-on-rails - 如何测试变量是否已定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19450086/

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