ruby中nil?, empty? and blank?
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?的更多相关文章
- ruby中nil?, empty? and blank?的选择
In Ruby, you check with nil? if an object is nil: article = nil article.nil? # => true empty? che ...
- 【转】ruby中nil?, empty? and blank?的选择
In Ruby, you check with nil? if an object is nil:article = nil article.nil? # => true empty? chec ...
- Rails中nil? empty? blank? present?的区别
.nil? Ruby方法 .nil?方法被放置在Object类中,可以被任何对象调用,如果是nil则返回true 在Rails中只有nil对象才会返回true nil.nil? #=> true ...
- ruby : nil?, empty? and blank?的选择
article = nil article.nil? # => true empty? checks if an element - like a string or an array f.e. ...
- .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 ...
- Rails :.nil? , .empty?, .blank? .present? 的区别
.nil? , .empty?, .blank? .present? 的区别 首先这三个都是判空的. 而 .nil? 和 .empty? 是ruby的方法. .blank? 是rails的方法 .ni ...
- 在 Ruby 中执行 Shell 命令的 6 种方法
我们时常会与操作系统交互或在 Ruby 中执行 Shell 命令.Ruby为我们提供了完成该任务的诸多方法. Exec Kernel#exec 通过执行给定的命令来替换当前进程,例如: $ irb & ...
- [翻译]理解Ruby中的blocks,Procs和lambda
原文出处:Understanding Ruby Blocks, Procs and Lambdas blocks,Procs和lambda(在编程领域被称为闭包)是Ruby中很强大的特性,也是最容易引 ...
- 理解Ruby中的作用域
作用域对于Ruby以及其它编程语言都是一个需要理解的至关重要的基础知识.在我刚开始学习ruby的时候遇到很多诸如变量未定义.变量没有正确赋值之类的问题,归根结底是因为自己对于ruby作用域的了解不够, ...
随机推荐
- HTML图片热区 map area 标签
实例 <img src ="planets.gif" alt="Planets" usemap ="#planetmap" /> ...
- 【转载】postgreSQL在linux中安装详解
.编译环境 Linux: CentOS 5.5 gcc: 4.1.2 1. 安装PostgreSQL 1) 解压postgresql-9.1.7.tar.bz2 #tar jxvf postgresq ...
- Oracle大数据表的分表处理
1.首先给大数据表创建rownum序列号 --增加序列号字段 alter table TEST add xlh number; --填充序列号 update TEST set xlh = rownum ...
- review17
关于构造方法Random(long seed)的理解 无参构造方法使用的默认参数是系统当前的毫秒数.使用同一数值的种子参数,生成的随机数也是一样的. 代码如下所示: import java.util. ...
- MVC。
mvc 开启客户端 和 远程验证 <appSettings> <add key="ClientValidationEnabled" value="tru ...
- MVC3 学习总结一(未发布)
MVC: Model,View,Control 设置View中的数据 1. 返回model,View中强类型化 Control: public ActionResult Browse(strin ...
- 关闭 Windows Defender
关闭 Windows Defender Win+R,输入 gpedit.msc 回车,打开组策略编辑器 展开[计算机设置]-[管理模板]-[Windows 组件]-[Windows Defender] ...
- vue配置stylus
首先Vue-init webapck 随便一个name vue项目创建好之后 cnpm i (利用淘宝镜像) cnpm i stylus stylus-loader -D 进行安装和配置 styl常 ...
- deep learning 自编码算法详细理解与代码实现(超详细)
在有监督学习中,训练样本是有类别标签的.现在假设我们只有一个没有带类别标签的训练样本集合 ,其中 .自编码神经网络是一种无监督学习算法,它使用了反向传播算法,并让目标值等于输入值,比如 .下图是一个自 ...
- python--*args和**kwargs可变参数
先来看个例子: #! /usr/bin/env python #coding=utf-8 def foo(*args, **kwargs): print('args=',args) print('kw ...