ruby .nil?、.blank 之间的区别?和.空?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11029256/
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-06 05:12:37  来源:igfitidea点击:

Difference between .nil?, .blank? and .empty?

rubyruby-on-rails-3object

提问by Someth Victory

Possible Duplicate:
A concise explanation of nil v. empty v. blank in Ruby on Rails

可能的重复:
Ruby on Rails 中 nil v. empty v. blank 的简明解释

Can anyone tell me the difference between nil?, blank?and empty?in Ruby?

谁能告诉我nil?,blank?empty?Ruby 中的区别?

回答by mikej

In Ruby, nilin an object (a single instance of the class NilClass). This means that methods can be called on it. nil?is a standard method in Ruby that can be called on allobjects and returns truefor the nilobject and falsefor anything else.

在 Ruby 中,nil在一个对象(类的单个实例NilClass)中。这意味着可以对其调用方法。nil?是 Ruby 中的标准方法,可以在所有对象上调用并true为该nil对象和false其他任何对象返回。

empty?is a standard Ruby method on someobjects like Arrays, Hashes and Strings. Its exact behaviour will depend on the specific object, but typically it returns trueif the object contains no elements.

empty?一些对象(如数组、哈希和字符串)上的标准 Ruby 方法。它的确切行为将取决于特定对象,但通常true如果对象不包含任何元素,它就会返回。

blank?is not a standard Ruby method but is added to allobjects by Rails and returns truefor nil, false, empty, or a whitespace string.

blank?是不是一个标准的Ruby方法,但是被添加到所有由导轨和返回的对象truenilfalse,清空一个空白串,或。

Because empty?is not defined for all objects you would get a NoMethodErrorif you called empty?on nilso to avoid having to write things like if x.nil? || x.empty?Rails adds the blank?method.

因为empty?不是为所有对象定义的,NoMethodError如果你调用它empty?,你会得到一个,nil所以避免编写类似if x.nil? || x.empty?Rails 添加blank?方法的东西。



After answering, I found an earlier question, "How to understand nil vs. empty vs. blank in Rails (and Ruby)", so you should check the answers to that too.

在回答之后,我发现了一个更早的问题,“如何在 Rails(和 Ruby)中理解 nil 与空与空白”,所以你也应该检查一下答案。

回答by fl00r

Feel it ;)

感受一下;)

NIL?

零?

nil.nil?
#=> true
[].nil?
#=> false
"".nil?
#=> false
" ".nil?
#=> false

EMPTY?

空的?

[].empty?
#=> true
nil.empty?
#=> undefined method
"".empty?
#=> true
" ".empty?
#=> false

BLANK?

空白的?

[].blank?
#=> true
nil.blank?
#=> true
"".blank?
#=> true
" ".blank?
#=> true

回答by Ribtoks

Any Ruby variable is an object, so it can be uninitialized/unset (set to nil). nil?method returns true if it is not initialized:

任何 Ruby 变量都是一个对象,因此它可以是未初始化/未设置的(设置为 nil)。nil?如果未初始化,则方法返回 true:

b = nil
b.nil? # true
b = 'string value'
b.nil? # false

Arrays, strings, streams in Ruby can contain no data, so they can be empty. The empty?method returns true if so:

Ruby 中的数组、字符串、流不能包含任何数据,因此它们可以为空。empty?如果是这样,该方法返回 true:

array = []
array.empty? # true
array << 5 << 4 # [5, 4]
array.empty? # false

string = "" # empty line
string.empty? # true

blank?is Active Support specific method (available in any object) and is available in Ruby On Rails with Active Support. It returns true if an object is nil or empty.

blank?是 Active Support 特定的方法(在任何对象中可用)并且在具有 Active Support 的 Ruby On Rails 中可用。如果对象为 nil 或为空,则返回 true。