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. VS2010+Oracle11+Entity Framework4.1环境搭建及常见问题

    在微软的实体数据模型中存在四种查询方式: SQL字符串:Linq:Linq to SQL:Linq to Entity(ESQL) 对于Linq SQL目前微软虽然仍在支持,但微软已经声明不再推荐. ...

  2. ThinkPad T430i,如何将WIN8换成WIN7???

    1. 启动时不断点击键盘上的F1键,进入BIOS 界面选择“Restart”→把 “OS Optimized Default”设置为 “disabled” ,(OS Optimized Default ...

  3. Java泛型与Restlet客户端

    写一个与restlet服务器通信的客户端类,用于测试通信是否成功,并且进行交互.为了方便其他人使用,于是,写一个通用的方法封装起来,可是中途却放生了一些问题. 按照正常写法,顺序走下来是这样的: pu ...

  4. MongoDB与SqlSugar与Ado.Net对比

    SqlSugar NET 4.+ & .NET CORE 高性能 轻量级 ORM框架,众多.NET框架中最容易使用的数据库访问技术 MongoDB与MySql的安装省略...虽然遇到了一些意外 ...

  5. Code First 更新数据库 记录

    每次都会忘记这个,所以记录一下 第一步:打开程序包管理控制台 第二步:启动迁移配置 第三步: 更新数据库的迁移的名称 因为设置了多个context,所以要指定更新的是哪一个库. 如果没有指定,会出现下 ...

  6. C#HttpUtility.UrlEncode 大写问题

    工作上和另一个公司对接,调对方的api需要用到md5加密,加密前要使用HttpUtility.UrlEncode,对方接口一直返回验证错误,定位了问题发现是中文编码使用HttpUtility.UrlE ...

  7. K8S+GitLab-自动化分布式部署ASP.NET Core(三) 更新镜像版本并部署到K8S上

    一.介绍 前一篇,介绍了ASP.NET Core部署到K8S上,下面介绍我们在发布新一版本中怎么通过Gitlab CI自动给镜像打版本并部署到K8S上. 二.我们通过GitLab CI/CD 变量 不 ...

  8. Zimbra无需登录RCE漏洞利用

    2019年3月13号,一名国外的安全研究员在他的博客上公布了zimbra RCE漏洞相关信息,但其中并未提到一些漏洞利用细节. 经过一段时间努力,根据网上各位大牛的分析和我自己的理解,在此我将整个漏洞 ...

  9. Open GL的学习路程

    一.EGL的使用 1.Dispaly与原生窗口的链接 -EGLDisplay  eglGetDisplay -EGLBoolean  egllinitialize 2.Surface 配置和创建sur ...

  10. 读DEDECMS找后台目录有感

    本文作者:红日安全团队——Mochazz 早上看了先知论坛的这篇文章:解决DEDECMS历史难题–找后台目录 不得不说作者思路确实巧妙,作者巧妙的利用了Windows FindFirstFile和织梦 ...