not_nil 有 Ruby 或 Ruby-ism 吗?与零相反?方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4012775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:28:19  来源:igfitidea点击:

Is there a Ruby, or Ruby-ism for not_nil? opposite of nil? method?

ruby-on-railsrubynull

提问by berkes

I am not experienced in Ruby, so my code feels "ugly" and not idiomatic:

我在 Ruby 方面没有经验,所以我的代码感觉“丑陋”而不是惯用的:

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?

但是找不到这样一种相反的方法 nil?

采纳答案by lwe

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

当您使用 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

回答by Samo

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.

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

Update

更新

If you're using Rails, you can make this read more nicely by using the present?method:

如果您使用的是 Rails,则可以使用以下present?方法更好地阅读:

def logged_in?
  user.present?
end

回答by A Fader Darkly

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

当心其他答案present?作为您问题的答案。

present?is the opposite of blank?in rails.

present?blank?in rails相反。

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

present?检查是否存在有意义的值。这些事情可能无法通过present?检查:

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

Whereas a !nil?check gives:

!nil?支票给出:

!"".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.

nil?检查对象是否实际上是nil. 其他任何东西:一个空字符串,0, false,无论如何,不​​是nil

present?is very useful, but definitely not the opposite of nil?. Confusing the two can lead to unexpected errors.

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

For your use case present?will work, but it's always wise to be aware of the difference.

因为您的用例会present?起作用,但了解差异总是明智的。

回答by Geo

Maybe this could be an approach:

也许这可能是一种方法:

class Object
  def not_nil?
    !nil?
  end
end

回答by Bitterzoet

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.

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

回答by Ian

I arrived at this question looking for an object method, so that I could use the Symbol#to_procshorthandinstead of a block; I find arr.find(&:not_nil?)somewhat more readable than arr.find { |e| !e.nil? }.

我遇到这个问题是为了寻找一个对象方法,这样我就可以使用Symbol#to_proc简写而不是块;我发现arr.find(&:not_nil?)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:

我找到的方法是Object#itself. 在我的用法中,我想在 key 的散列中找到值name,在某些情况下,该 key 被意外地大写为Name. 那一行是这样的:

# 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.

正如其他答案中所述,在您测试布尔值的情况下,这将失败。

回答by Christopher Oezbek

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

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

def logged_in?
  user.nil?.!
end

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

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