not_nil 有 Ruby 或 Ruby-ism 吗?零的反义词?方法?

发布于 2024-09-29 02:11:04 字数 216 浏览 3 评论 0原文

我没有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

请叫√我孤独 2024-10-06 02:11:04

您似乎过于关心布尔值。

def logged_in?
  user
end

如果用户为零,则logged_in?将返回一个“falsey”值。否则,它将返回一个对象。在 Ruby 中,我们不需要返回 true 或 false,因为我们像 JavaScript 一样有“true”和“falsey”值。

更新

如果您使用 Rails,则可以使用 present? 方法使此内容读起来更顺畅:

def logged_in?
  user.present?
end

You seem overly concerned with booleans.

def logged_in?
  user
end

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:

def logged_in?
  user.present?
end
魔法少女 2024-10-06 02:11:04

当您使用 ActiveSupport 时,有 user.present? http ://api.rubyonrails.org/classes/Object.html#method-i-present%3F,仅检查非零,为什么不使用

def logged_in?
  user # or !!user if you really want boolean's
end

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

def logged_in?
  user # or !!user if you really want boolean's
end
大海や 2024-10-06 02:11:04

请注意其他以 present? 作为问题答案的答案。

present? 与 Rails 中的 blank? 相反。

present? 检查是否存在有意义的值。这些东西可能会导致 present? 检查失败:

"".present? # false
"    ".present? # false
[].present? # false
false.present? # false
YourActiveRecordModel.where("false = true").present? # false

!nil? 检查给出:

!"".nil? # true
!"    ".nil? # true
![].nil? # true
!false.nil? # true
!YourActiveRecordModel.where("false = true").nil? # true

nil? 检查对象是否实际上是 nil。其他任何东西:空字符串、0false 等等,都不是 nil

present? 非常有用,但绝对不是 nil? 的反义词。混淆两者可能会导致意外错误。

对于您的用例,present? 会起作用,但意识到其中的差异总是明智的。

Beware other answers presenting present? as an answer to your question.

present? is the opposite of blank? in rails.

present? checks if there is a meaningful value. These things can fail a present? check:

"".present? # false
"    ".present? # false
[].present? # false
false.present? # false
YourActiveRecordModel.where("false = true").present? # false

Whereas a !nil? check gives:

!"".nil? # true
!"    ".nil? # true
![].nil? # true
!false.nil? # true
!YourActiveRecordModel.where("false = true").nil? # true

nil? checks if an object is actually nil. Anything else: an empty string, 0, false, whatever, is not nil.

present? is very useful, but definitely not the opposite of nil?. 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.

池木 2024-10-06 02:11:04

也许这可能是一种方法:

class Object
  def not_nil?
    !nil?
  end
end

Maybe this could be an approach:

class Object
  def not_nil?
    !nil?
  end
end
吻安 2024-10-06 02:11:04

我可以在 nil? 方法的结果上提供 Ruby 式的 ! 方法吗?

def logged_in?
  user.nil?.!
end

如此深奥以至于 RubyMine IDE 会将其标记为错误。 ;-)

May I offer the Ruby-esque ! method on the result from the nil? method.

def logged_in?
  user.nil?.!
end

So esoteric that RubyMine IDE will flag it as an error. ;-)

仄言 2024-10-06 02:11:04

您可以只使用以下内容:

if object
  p "object exists"
else
  p "object does not exist"
end

这不仅适用于 nil,还适用于 false 等,因此您应该测试它是否适用于您的用例。

You can just use the following:

if object
  p "object exists"
else
  p "object does not exist"
end

This does not only work for nil but also false etc, so you should test to see if it works out in your usecase.

紫﹏色ふ单纯 2024-10-06 02:11:04

我在寻找对象方法时遇到了这个问题,这样我就可以使用 Symbol#to_proc简写而不是块;我发现 arr.find(&:not_nil?) 比 arr.find { |e| 更具可读性!e.nil? }。

我找到的方法是 Object#itself< /代码>。在我的使用中,我想在哈希值中找到键 name 的值,在某些情况下,该键被意外地大写为 Name。这句话如下:

# Extract values for several possible keys 
#   and find the first non-nil one
["Name", "name"].map { |k| my_hash[k] }.find(&:itself)

正如其他答案中所述,如果您正在测试布尔值,这将严重失败。

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 find arr.find(&:not_nil?) somewhat more readable than arr.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 key name, where in some cases that key was accidentally capitalized as Name. That one-liner is as follows:

# Extract values for several possible keys 
#   and find the first non-nil one
["Name", "name"].map { |k| my_hash[k] }.find(&:itself)

As noted in other answers, this will fail spectacularly in cases where you are testing a boolean.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文