In Ruby, you check with nil? if an object is nil:

article = nil
article.nil? # => true

empty? checks if an element - like a string or an array f.e. - is empty:

# Array
[].empty? #=> true
# String
"".empty? #=> true

Rails adds the method blank? to the Object class:

An object is blank if it‘s false, empty, or a whitespace string. For example, "", " ", nil, [], and {} are blank.

This simplifies

if !address.nil? && !address.empty?

to

if !address.blank?

.nil?

- It is Ruby method
- It can be used on any object and is true if the object is nil.
- "Only the object nil responds true to nil?" - RailsAPI nil.nil? = true
anthing_else.nil? = false
a = nil
a.nil? = true
“”.nil = false .empty? - It is Ruby method
- 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 "".empty = true
" ".empty? = false .blank? - It is Rails method
- 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 - It also evaluates true on strings which are non-empty but contain only whitespace: "  ".blank? == true"  ".empty? == false Quick tip: !obj.blank? == obj.present? activesupport/lib/active_support/core_ext/object/blank.rb, line 17 # (Ruby 1.9) def present?
 !blank?
end

ruby中nil?, empty? and blank?的选择的更多相关文章

  1. 【转】ruby中nil?, empty? and blank?的选择

    In Ruby, you check with nil? if an object is nil:article = nil article.nil? # => true empty? chec ...

  2. ruby中nil?, empty? and blank?

    In Ruby, you check with nil? if an object is nil: article = nil article.nil? # => true empty? che ...

  3. ruby : nil?, empty? and blank?的选择

    article = nil article.nil? # => true empty? checks if an element - like a string or an array f.e. ...

  4. Rails中nil? empty? blank? present?的区别

    .nil? Ruby方法 .nil?方法被放置在Object类中,可以被任何对象调用,如果是nil则返回true 在Rails中只有nil对象才会返回true nil.nil? #=> true ...

  5. .nil? .empty? .blank? .present? in Ruby on Rails

    We get confused when there are many options to choose from. Same is the case when it comes to use an ...

  6. Rails :.nil? , .empty?, .blank? .present? 的区别

    .nil? , .empty?, .blank? .present? 的区别 首先这三个都是判空的. 而 .nil? 和 .empty? 是ruby的方法. .blank? 是rails的方法 .ni ...

  7. ruby中symbol

    Symbol 是什么 Ruby 是一个强大的面向对象脚本语言(本文所用 Ruby 版本为1.8.6),在 Ruby 中 Symbol 表示“名字”,比如字符串的名字,标识符的名字. 创建一个 Symb ...

  8. 在 Ruby 中执行 Shell 命令的 6 种方法

    我们时常会与操作系统交互或在 Ruby 中执行 Shell 命令.Ruby为我们提供了完成该任务的诸多方法. Exec Kernel#exec 通过执行给定的命令来替换当前进程,例如: $ irb & ...

  9. ruby中的可调用对象--proc和lamdba

    ruby中将块转变成对象的三种方法 ruby中的大部分东西都是对象,但是块不是.那么,如果你想存下来一个块,方便以后使用,你就需要一个对象.ruby中有三种方法,把块转换成可以利用的对象. Proc. ...

随机推荐

  1. Difference between the Bill of distribution and sourcing rule.

        https://forums.oracle.com/thread/936768 This is from a users guide  Oracle Supply Chain Planning ...

  2. 抱SQL SERVER大腿之我爱用视图(对大数据量的管理)

    我们拥有一个巨大的表,两千多万条记录.也许在行家眼里,两千多万条记录顶多算条毛,不过这条毛也忒粗壮了一点:我们的数据库占用的空间已经达到5G多了.不要以为是日志文件在搞鬼,日志文件可以自动收缩的,最多 ...

  3. golang array, slice, string笔记

    本来想写一篇关于golang io的笔记,但是在学习io之前必须了解array, slice, string概念,因此将在下篇写golang io.   array: 数组的长度是该数组类型的一部分, ...

  4. ie下警告console未定义

    低版本IE6/7/8/9浏览器没有定义console对象,所以代码会中断执行.自己测试,ie11也没有(打开控制台的情况下可以用) 可以用如下代码完美解决. window.console = wind ...

  5. dotNet core 应用部署centos

    ---恢复内容开始--- 阅读目录 需要安装的插件以及支撑架构 安装dotnetSDK 安装jexus 安装supervisord 遇到问题汇总 注意事项.扩展延伸 需要安装的插件以及支撑架构 1.d ...

  6. BroadcastReceive的使用

    一.注册方式 intent.setAction("BC_One"); 1.静态注册 <receiver  android:name = "继承BroadcastRe ...

  7. IOS - IPhone或IPAD,如何恢复出厂操作系统?

    IPhone或IPAD的操作系统都是IOS,如果IPhone或IPAD越狱,或其它原因导致不能正常使用了,恢复出厂设置能够得到一个可以正常工作的设备.恢复的方法也比较简单,就是用iTunes,一般情况 ...

  8. Java Web相关技术(汇聚页)

    Java Web相关技术(汇聚页) 初学Java Web(2)——搭建Java Web开发环境

  9. 双向链表的实现——java

    实现类: /** * Java 实现的双向链表. * 注:java自带的集合包中有实现双向链表,路径是:java.util.LinkedList * * @author skywang * @date ...

  10. Codeforces Round #426 (Div. 2)A B C题+赛后小结

    最近比赛有点多,可是好像每场比赛都是被虐,单纯磨砺心态的作用.最近讲的内容也有点多,即便是点到为止很浅显的版块,刷了专题之后的状态还是~"咦,能做,可是并没有把握能A啊".每场网络 ...