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? che ...

  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 中执行 Shell 命令的 6 种方法

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

  8. [翻译]理解Ruby中的blocks,Procs和lambda

    原文出处:Understanding Ruby Blocks, Procs and Lambdas blocks,Procs和lambda(在编程领域被称为闭包)是Ruby中很强大的特性,也是最容易引 ...

  9. 理解Ruby中的作用域

    作用域对于Ruby以及其它编程语言都是一个需要理解的至关重要的基础知识.在我刚开始学习ruby的时候遇到很多诸如变量未定义.变量没有正确赋值之类的问题,归根结底是因为自己对于ruby作用域的了解不够, ...

随机推荐

  1. 使用kibana进行简单的CRUD和版本控制

    使用: ##创建文档之前先创建索引 PUT /toov5 ##查询索引 GET /toov5 ##创建文档 /索引/类型/id PUT /toov5/user/1 { "name" ...

  2. 搭建TXManager分布式事务协调者

    事务分组id 缓存到redis 需要配置连接到自己的 redis地址 启动后:

  3. 搭建confluence参考文献

    https://www.cnblogs.com/kevingrace/p/7607442.html https://yq.aliyun.com/articles/144747?t=t1 jira: h ...

  4. Mysql 利用小工具源码

    #include "StdAfx.h" #include "Sql.h" #include <windows.h> #include <std ...

  5. MYSQL进阶学习笔记一:MySQL编码设定,会话变量和全局变量!(视频序号:进阶_1-3)

    知识点一:MySQL编码设定(1-2) 服务器编码设定: 查看MySQL服务器端的编码格式: SHOW VARIABLES LIKE ‘char%’; 设定编码格式: SET NAMES ‘utf8’ ...

  6. hdoj1006--Tick and Tick

    Problem Description The three hands of the clock are rotating every second and meeting each other ma ...

  7. Membership如何得到当前登录的用户名称

        System.Web.Security.Membership     Membership.GetUser().UserName可以得到当前登录的用户信息.

  8. Mssql 跨域查询

    有数据库test1和数据库test2.其中test1中有表 table1.table2:test2 中有表 table1.三个表的字段都为为:id.xingming.shijian.shuliang. ...

  9. 安装Xcode 7 beta后Xcode 6崩溃的问题

    最新解决方案:把OSX El Capitan升级到Beta 7  (15A263e),Xcode6可使用! 解决方案:http://stackoverflow.com/questions/318035 ...

  10. 51nod 1163 贪心

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1163 1163 最高的奖励 基准时间限制:1 秒 空间限制:131072 ...