rails nil blank

up vote 7 down vote favorite
1

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:

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.

Is there anything missing, or a tighter comparison that can be made?

link | flag

 
 
  
add comment
 
start a bounty

offer   of my own reputation for an answer to this question

     What is bounty?

5 Answers

up vote 17 down vote accepted

.nil?

can be used on any object and is true if the object is nil

.empty?

can be used on strings, arrays and hashes and returns true if:

  • String length == 0
  • Array length == 0
  • Hash length == 0

running .empty? on something that is nil will throw a NoMethodError

That is where .blank? comes in. It is implemented by Rails and will operate on any object as well as work like .empty? on strings, arrays and hashes.

nil.blank? == true
[].blank? == true
{}.blank? == true
"".blank? == true
5.blank? == false

.blank? also evaluates true on strings which are non-empty but contain only whitespace.

"  ".blank? == true
"  ".empty? == false
link | flag
 
 
1
up vote
 
flag
As mentioned in the question, some non-empty strings count as blank. –  Andrew Grimm May 20 '09 at 23:20
 
up vote
 
flag
Thanks for catching that. –  Corban Brook May 21 '09 at 19:47
add comment
up vote 2 down vote

One difference is that .nil? and .empty? are methods that are provided by the programming language Ruby, whereas .blank? is something added by the web development framework Rails.

link | flag
 
 
  
add comment
up vote 2 down vote

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 something or unless something then use blank? where needed since it seems to work everywhere.

link | flag
 
 
2
up vote
 
flag
.any? doesn't work with strings in ruby 1.9, as .any? requires enumerable, and string#each by itself doesn't work in ruby 1.9. –  Andrew Grimm May 20 '09 at 0:59
add comment
up vote 0 down vote

Just a little note about the any? recommendation: He's right that it's generally equivalent to !empty?. However, any? will return true to a string of just whitespace (ala " ").

And of course, see the 1.9 comment above, too.

link | flag
 
 
  
add comment
up vote 0 down vote

Quick tip: !obj.blank? == obj.present?

Can be handy/easier on the eyes in some expressions

link | flag
posted on 2010-10-29 22:32  lexus 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lexus/archive/2010/10/29/1864821.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值