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. 解决由AJAX请求时forms认证实效的重新认证问题

    前言: 当用AJAX请求一个资源时,服务器检查到认证过期,会重新返回302,通过HTTP抓包,是看到请求了登录页面的,但是JS是不会进行跳转到登录页面. 使用环境: ASP.NET MVC 4 JQU ...

  2. LeetCode135:Candy

    题目: There are N children standing in a line. Each child is assigned a rating value. You are giving c ...

  3. TempDB--临时表的缓存

    --========================================================================== 在博客园看到一篇文章<SQLServer ...

  4. <mvc:annotation-driven />到底帮我们做了啥

    一句 <mvc:annotation-driven />实际做了以下工作:(不包括添加自己定义的拦截器) 我们了解这些之后,对Spring3 MVC的控制力就更强大了,想改哪就改哪里. s ...

  5. hbase启动异常的慢

    hbase启动慢 hbase启动非常慢,要几个小时,查看日志,发现有如下异常信息: 2016-12-02 22:39:09,365 ERROR [RS_LOG_REPLAY_OPS-db-dn001: ...

  6. Oracle数据库中 to_date()与24小时制表示法及mm分钟的显示

      一.在使用Oracle的to_date函数来做日期转换时,时候也许会直接的采用“yyyy-MM-dd HH:mm:ss”的格式作为格式进行转换,但是在Oracle中会引起错误:“ORA 01810 ...

  7. 使用chosen插件实现多级联动和置位

    使用chosen插件实现多级联动和置位 首先写好第一个select,加上onchage属性之后,写onchange方法. <select data-placeholder="选择省份. ...

  8. 基于MVC框架layui分页控件实现前端分页信息写法

    详细链接:https://shop499704308.taobao.com/?spm=a1z38n.10677092.card.11.594c1debsAGeak@{ ViewBag.Title = ...

  9. Lucene.net 全文检索数据库

    #define Search using Lucene.Net.Analysis; using Lucene.Net.Analysis.Tokenattributes; using Lucene.Ne ...

  10. XCode - 无法对iPhone真机调试的解决方法!

    OSX:10.14 XCode:10.1 真机:iPhone 4S 错误很多啊,并非编译错误,编译已经成功了,但是无法安装到真机,我真不理解啊!!由于真的没有想到能够解决,有的错误没有截图,先看部分错 ...