如何理解 Ruby 中的 nil vs. empty vs. blank
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/885414/
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
How to understand nil vs. empty vs. blank in Ruby
提问by Arrel
I find myself repeatedly looking for a clear definition of the differences of nil?, blank?, and empty?in Ruby on Rails. Here's the closest I've come:
我发现自己反复寻找的差异明确的定义nil?,blank?以及empty?在Ruby on Rails的。这是我最近的一次:
blank?objects are false, empty, or a whitespace string. For example,""," ",nil,[], and{}are blank.nil?objects are instances of NilClass.empty?objects are class-specific, and the definition varies from class to class. A string is empty if it has no characters, and an array is empty if it contains no items.
blank?对象为假、空或空白字符串。例如""," ",nil,[],和{}都是空白。nil?对象是 NilClass 的实例。empty?对象是特定于类的,并且定义因类而异。如果字符串没有字符,则字符串为空;如果数组不包含任何项,则数组为空。
Is there anything missing, or a tighter comparison that can be made?
有什么遗漏,或者可以进行更严格的比较吗?
回答by Corban Brook
.nil?can be used on any object and is true if the object is nil.
.nil?可以在任何对象上使用,如果对象为零则为真。
.empty?can be used on strings, arrays and hashes and returns true if:
.empty?可用于字符串、数组和散列,并在以下情况下返回 true:
- String length == 0
- Array length == 0
- Hash length == 0
- 字符串长度 == 0
- 数组长度 == 0
- 哈希长度 == 0
Running .empty?on something that is nil will throw a NoMethodError.
运行.empty?在 nil 的东西上会抛出一个NoMethodError.
That is where .blank?comes in. It is implemented by Railsand will operate on any object as well as work like .empty?on strings, arrays and hashes.
这就是它的.blank?用武之地。它是由 Rails 实现的,可以对任何对象进行操作,也可以像.empty?处理字符串、数组和散列一样工作。
nil.blank? == true
false.blank? == true
[].blank? == true
{}.blank? == true
"".blank? == true
5.blank? == false
0.blank? == false
.blank?also evaluates true on strings which are non-empty but contain only whitespace:
.blank?还对非空但仅包含空格的字符串计算 true:
" ".blank? == true
" ".empty? == false
Rails also provides.present?, which returns the negation of .blank?.
Rails 还提供了.present?,它返回 的否定.blank?。
Array gotcha: blank?will return falseeven if all elementsof an array are blank. To determine blankness in this case, use all?with blank?, for example:
Array gotcha:即使数组的所有元素都为空,blank?也会返回。在这种情况下,要确定空白,请使用with ,例如:falseall?blank?
[ nil, '' ].blank? == false
[ nil, '' ].all? &:blank? == true
回答by Julian Popov
I made this useful table with all the cases:
我在所有情况下制作了这张有用的表格:


blank?, present?are provided by Rails.
blank?,present?由 Rails 提供。
回答by Sibevin Wang
回答by Alexander Malfait
Quick tip: !obj.blank? == obj.present?
小建议: !obj.blank? == obj.present?
Can be handy/easier on the eyes in some expressions
在某些表情中可以方便/更容易地看到眼睛
回答by Andrew Grimm
回答by steve
- Everything that is
nil?isblank? - Everything that is
empty?isblank? - Nothing that is
empty?isnil? - Nothing that is
nil?isempty?
- 一切
nil?是blank? - 一切
empty?是blank? - 什么都不
empty?是nil? - 什么都不
nil?是empty?
tl;dr -- only use blank?& present?unless you want to distinguish between ""and " "
tl;dr——除非你想区分和blank?,present?否则只使用&""" "
回答by Alan H
A special case is when trying to assess if a boolean value is nil:
一个特殊情况是在尝试评估布尔值是否为 nil 时:
false.present? == false
false.blank? == true
false.nil? == false
In this case the recommendation would be to use .nil?
在这种情况下,建议使用 .nil?
回答by davemyron
Just a little note about the any?recommendation: He's right that it's generallyequivalent to !empty?. However, any?will return trueto a string of just whitespace (ala " ").
关于any?建议的一点说明:他是对的,它通常等同于!empty?. 但是,any?将返回true一串只有空格 (ala " ")的字符串。
And of course, see the 1.9 comment above, too.
当然,也请参阅上面的 1.9 评论。
回答by davemyron
Don't forget any?which is generally !empty?. In Rails I typically check for the presence of something at the end of a statement with if somethingor unless somethingthen use blank?where needed since it seems to work everywhere.
不要忘记any?通常是哪个!empty?。在Rails我通常检查的东西存在于一个语句的结束if something或unless something再使用blank?在需要的地方,因为它似乎无处不工作。
回答by neha
nil?is a standard Ruby method that can be called on all objects and returns trueif the object is nil:
nil?是一个标准的 Ruby 方法,可以在所有对象上调用,true如果对象是nil:
b = nil
b.nil? # => true
empty?is a standard Ruby method that can be called on some objects such as Strings, Arrays and Hashes and returns trueif these objects contain no element:
empty?是一个标准的 Ruby 方法,可以在一些对象上调用,如字符串、数组和哈希,true如果这些对象不包含元素,则返回:
a = []
a.empty? # => true
b = ["2","4"]
b.empty? # => false
empty?cannot be called on nilobjects.
empty?不能在nil对象上调用。
blank?is a Rails method that can be called on nilobjects as well as empty objects.
blank?是一个 Rails 方法,可以在nil对象和空对象上调用。

