在调用之前检查是否定义了 method/property
Check if a method/property is defined or not before calling
我是 RoR 的新手。我想弄清楚如何检查环境文件中是否定义了 属性(在本例中为 development.rb)。
我们在 development.rb 文件中定义了一个 属性,类似于:
config.user = 'test-user'
现在在代码中,我通过调用来使用它:
Rails.application.config.user
这给了我所需的值。
但问题是这个配置有时可能会被禁用。所以,我想在分配它之前检查这个 属性 是否被定义。
像
user_name = (if Rails.application.config.user is available)?
Rails.application.config.user : 'some_other_value'
我试过定义了吗?和 respond_to 但没有用。
任何 help/suggestions 都表示赞赏。谢谢!
如果在每个环境中都定义了一个config.user
,但有时它有值,有时没有,例如它可能是nil
或一个空字符串,你可以使用 present?:
Rails.application.config.user.present?
如果没有定义,上面的例子中会得到一个NoMethodError
,所以你可以挽救它:
begin
user_name = Rails.application.config.user.present? ? Rails.application.config.user : 'some_other_value'
rescue NoMethodError
user_name = 'some_other_value'
end
respond_to?
也应该有效,只要确保不要将它与 respond_to
混淆,后者是 is a Rails method。它可能看起来像这样:
if Rails.application.config.respond_to?(:user) && Rails.application.config.user.present?
user_name = Rails.application.config.user
else
user_name = 'some_other_value'
end
如果您正在使用 rails(它实际上来自 active_support
)每个对象都会有一个 try
方法,它也可以执行您想要的操作:
user_name = Rails.application.config.try(:user)
而ruby 2.3给我们带来了&.
:
user_name = Rails.application.config&.user
请注意,在这两种情况下,如果 nil
不应该是有效的 user_name(因为 try
和 &.
,您可以隐式使用 return 值如果 config
不响应 user
),return 将为 nil):
user_name = Rails.application.config&.user || 'Guest' # ruby >= 2.3.0
user_name = Rails.application.config.try(:user) || 'Guest'
如果你调用那段代码超过两次(我的经验法则),你应该考虑将它提取到一个自己的方法中,例如Application#user_name
.
更正
事后,我认为 &.
可能不会像预期的那样工作,因为 config
可能不是零。这实际上取决于设置(user
是否已配置,但为空?config
是如何实现的?)。我保留了那部分答案,因为它可能对相关问题感兴趣(但请记住:您需要 ruby 2.3 或更高版本)。
我是 RoR 的新手。我想弄清楚如何检查环境文件中是否定义了 属性(在本例中为 development.rb)。
我们在 development.rb 文件中定义了一个 属性,类似于:
config.user = 'test-user'
现在在代码中,我通过调用来使用它:
Rails.application.config.user
这给了我所需的值。
但问题是这个配置有时可能会被禁用。所以,我想在分配它之前检查这个 属性 是否被定义。 像
user_name = (if Rails.application.config.user is available)?
Rails.application.config.user : 'some_other_value'
我试过定义了吗?和 respond_to 但没有用。
任何 help/suggestions 都表示赞赏。谢谢!
如果在每个环境中都定义了一个config.user
,但有时它有值,有时没有,例如它可能是nil
或一个空字符串,你可以使用 present?:
Rails.application.config.user.present?
如果没有定义,上面的例子中会得到一个NoMethodError
,所以你可以挽救它:
begin
user_name = Rails.application.config.user.present? ? Rails.application.config.user : 'some_other_value'
rescue NoMethodError
user_name = 'some_other_value'
end
respond_to?
也应该有效,只要确保不要将它与 respond_to
混淆,后者是 is a Rails method。它可能看起来像这样:
if Rails.application.config.respond_to?(:user) && Rails.application.config.user.present?
user_name = Rails.application.config.user
else
user_name = 'some_other_value'
end
如果您正在使用 rails(它实际上来自 active_support
)每个对象都会有一个 try
方法,它也可以执行您想要的操作:
user_name = Rails.application.config.try(:user)
而ruby 2.3给我们带来了&.
:
user_name = Rails.application.config&.user
请注意,在这两种情况下,如果 nil
不应该是有效的 user_name(因为 try
和 &.
,您可以隐式使用 return 值如果 config
不响应 user
),return 将为 nil):
user_name = Rails.application.config&.user || 'Guest' # ruby >= 2.3.0
user_name = Rails.application.config.try(:user) || 'Guest'
如果你调用那段代码超过两次(我的经验法则),你应该考虑将它提取到一个自己的方法中,例如Application#user_name
.
更正
事后,我认为 &.
可能不会像预期的那样工作,因为 config
可能不是零。这实际上取决于设置(user
是否已配置,但为空?config
是如何实现的?)。我保留了那部分答案,因为它可能对相关问题感兴趣(但请记住:您需要 ruby 2.3 或更高版本)。