English 中文(简体)
有没有Ruby,或者Ruby ism表示not_nil?与零相反?方法
原标题:Is there a Ruby, or Ruby-ism for not_nil? opposite of nil? method?

我没有使用Ruby的经验,所以我的代码感觉“丑陋”且不地道:

def logged_in?
  !user.nil?
end

我宁愿吃这样的

def logged_in?
  user.not_nil?
end

但找不到这样一个方法,即对立<code>nil

最佳回答

当您使用ActiveSupport时,是否存在user.presenthttp://api.rubyonrails.org/classes/Object.html#method-i-present%3F,只检查非零,为什么不使用

def logged_in?
  user # or !!user if you really want boolean s
end
问题回答

你似乎过于在意布尔。

def logged_in?
  user
end

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

更新

如果您正在使用Rails,您可以通过使用<code>present方法:

def logged_in?
  user.present?
end

小心其他呈现<code>的答案作为您问题的答案。

是否存在空白的对立面吗。

是否存在检查是否存在有意义的值。这些东西会让礼物失败吗检查:

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

而一个<code>!零检查给出:

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

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

是否存在非常有用,但绝对不是nil的反面。混淆两者可能会导致意外的错误。

对于您的用例<code>是否存在会起作用,但明智的做法是要注意其中的区别。

也许这可能是一种方法:

class Object
  def not_nil?
    !nil?
  end
end

我可以提供Ruby风格的<code>吗方法对nil方法。

def logged_in?
  user.nil?.!
end

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

您只需使用以下内容:

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

这不仅适用于nil,也适用于false等,所以您应该测试它是否在您的用例中有效。

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

我找到的方法是对象#本身。在我的使用中,我想在键name的哈希中找到值,在某些情况下,该键意外地大写为name。这一行如下:

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

其他答案,在测试布尔值的情况下,这将非常失败。





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...