not_nil 有 Ruby 或 Ruby-ism 吗?零的反义词?方法?
我没有 Ruby 经验,所以我的代码感觉“丑陋”并且不惯用:
def logged_in?
!user.nil?
end
我宁愿有类似的东西
def logged_in?
user.not_nil?
end
但找不到这样一个与 nil?
相反的方法
I am not experienced in Ruby, so my code feels "ugly" and not idiomatic:
def logged_in?
!user.nil?
end
I'd rather have something like
def logged_in?
user.not_nil?
end
But cannot find such a method that opposites nil?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您似乎过于关心布尔值。
如果用户为零,则logged_in?将返回一个“falsey”值。否则,它将返回一个对象。在 Ruby 中,我们不需要返回 true 或 false,因为我们像 JavaScript 一样有“true”和“falsey”值。
更新
如果您使用 Rails,则可以使用
present?
方法使此内容读起来更顺畅:You seem overly concerned with booleans.
If the user is nil, then logged_in? will return a "falsey" value. Otherwise, it will return an object. In Ruby we don't need to return true or false, since we have "truthy" and "falsey" values like in JavaScript.
Update
If you're using Rails, you can make this read more nicely by using the
present?
method:当您使用 ActiveSupport 时,有
user.present?
http ://api.rubyonrails.org/classes/Object.html#method-i-present%3F,仅检查非零,为什么不使用when you're using ActiveSupport, there's
user.present?
http://api.rubyonrails.org/classes/Object.html#method-i-present%3F, to check just for non-nil, why not use请注意其他以
present?
作为问题答案的答案。present?
与 Rails 中的blank?
相反。present?
检查是否存在有意义的值。这些东西可能会导致present?
检查失败:而
!nil?
检查给出:nil?
检查对象是否实际上是nil
。其他任何东西:空字符串、0
、false
等等,都不是nil
。present?
非常有用,但绝对不是nil?
的反义词。混淆两者可能会导致意外错误。对于您的用例,
present?
会起作用,但意识到其中的差异总是明智的。Beware other answers presenting
present?
as an answer to your question.present?
is the opposite ofblank?
in rails.present?
checks if there is a meaningful value. These things can fail apresent?
check:Whereas a
!nil?
check gives:nil?
checks if an object is actuallynil
. Anything else: an empty string,0
,false
, whatever, is notnil
.present?
is very useful, but definitely not the opposite ofnil?
. Confusing the two can lead to unexpected errors.For your use case
present?
will work, but it's always wise to be aware of the difference.也许这可能是一种方法:
Maybe this could be an approach:
我可以在
nil?
方法的结果上提供 Ruby 式的!
方法吗?如此深奥以至于 RubyMine IDE 会将其标记为错误。 ;-)
May I offer the Ruby-esque
!
method on the result from thenil?
method.So esoteric that RubyMine IDE will flag it as an error. ;-)
您可以只使用以下内容:
这不仅适用于 nil,还适用于 false 等,因此您应该测试它是否适用于您的用例。
You can just use the following:
This does not only work for nil but also false etc, so you should test to see if it works out in your usecase.
我在寻找对象方法时遇到了这个问题,这样我就可以使用
Symbol#to_proc
简写而不是块;我发现 arr.find(&:not_nil?) 比 arr.find { |e| 更具可读性!e.nil? }。我找到的方法是
Object#itself< /代码>
。在我的使用中,我想在哈希值中找到键
name
的值,在某些情况下,该键被意外地大写为Name
。这句话如下:正如其他答案中所述,如果您正在测试布尔值,这将严重失败。
I arrived at this question looking for an object method, so that I could use the
Symbol#to_proc
shorthand instead of a block; I findarr.find(&:not_nil?)
somewhat more readable thanarr.find { |e| !e.nil? }
.The method I found is
Object#itself
. In my usage, I wanted to find the value in a hash for the keyname
, where in some cases that key was accidentally capitalized asName
. That one-liner is as follows:As noted in other answers, this will fail spectacularly in cases where you are testing a boolean.