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

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

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

  4. .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 ...

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

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

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

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

  7. version can neither be null, empty nor blank

    在用mybatis-generator逆向生成mapper和DAO的时候,出现了这个错误. mybatis-generator:generate 原因是在pom.xml中我的mysql依赖没有写版本号 ...

  8. ruby基本语法(2)

    关于数组 Ruby数组中的数据类型可以不相同并且长度也是可变的.(好聪明啊感觉用的久了就会变笨了,除非你本来就是老手)比如下面的例子 Myarray=[1,2,“ruby”] Ruby也支持那种-1的 ...

  9. 如何选择前端框架:ANGULAR VS EMBER VS REACT

    最近一段时间是令前端工程师们非常兴奋的时期,因为三大Web框架陆续发布新版本,让我们见识到了更强大的Web框架.Ember2.0在2个月之前已经发布,从1.0升级到2.0非常简单.几周之前React发 ...

随机推荐

  1. 将时区格式的时间转换为易于阅读的标准格式"yyyy-MM-dd"

    Date的显示格式就是时区格式, String 标准格式 = new SimpleDateFormat("yyyy--MM--dd").format(new Date());

  2. No.2 CAS之SPNEGO+LDAP认证配置

    1.概述 本文先配置了SPNEGO认证,就是如果用户操作系统如果登陆了公司的Windows域,用户浏览器访问应用服务即可免登录. 然后如果不在域里的员工,用LDAP认证方式,输账号密码登陆. 参考文档 ...

  3. 【转】Native JavaScript Data-Binding

    原文转自:http://www.sellarafaeli.com/blog/native_javascript_data_binding Two-way data-binding is such an ...

  4. [转] 使用 MYSQLBINLOG 来恢复数据

     使用 MYSQLBINLOG 来恢复数据 2009-04-05 12:47:05 标签:mysql mysqlbinlog 恢复 数据库 数据 原创作品,允许转载,转载时请务必以超链接形式标明文章 ...

  5. HBASE学习笔记-初步印象

    HBASE概念: HBASE是一个分布式架构的数据库,通过对数据进行多层的分块打散储存.从而改写传统数据库的储存能力和读取速度. HBASE的集群服务器: HBASE的集群主要分为Zookeeper集 ...

  6. Redmine CodeReview

    Windows Bitnami Redmine通过自带必要的RoR依赖,解决了Redmine安装困难的问题,但也带来了很多Redmine的文档无法使用到Bitnami的问题.原因应该是很多命令不在用户 ...

  7. 火车头wordpress免费万能发布模块和接口

    火车头wordpress免费万能发布模块和接口实测可以用 http://www.ggfenxiang8.com/?p=263

  8. 基于redis排行榜的实战总结

    前言: 之前写过排行榜的设计和实现, 不同需求其背后的架构和设计模型也不一样. 平台差异, 有的立足于游戏平台, 为多个应用提供服务, 有的仅限于单个游戏.排名范围差异, 有的面向全局排名, 有的只做 ...

  9. 腾讯优测优分享 | Android适配中的一些特殊情况小结

    腾讯优测是专业的自动化测试平台,提供全面兼容适配测试,远程真机租用等多维度的测试服务! 作为一名"艰苦卓绝"的软件工程师,我在开发路上经常被各种奇葩情况虐的体无完肤...今天就想与 ...

  10. 排球比赛计分规则(P205页)

    排球比赛计分规则: 1.SPEC的目标是什么?SPEC的目标不包括什么? 为了让大家更加了解排球比赛. 2.SPEC用户和典型场景是什么? 用户:运动员,观众,教练 场景:排球赛场 3.SPEC用到哪 ...