ruby : nil?, empty? and blank?的选择
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 ...
- ruby中nil?, empty? and blank?
In Ruby, you check with nil? if an object is nil: article = nil article.nil? # => true empty? che ...
- .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 ...
- Rails中nil? empty? blank? present?的区别
.nil? Ruby方法 .nil?方法被放置在Object类中,可以被任何对象调用,如果是nil则返回true 在Rails中只有nil对象才会返回true nil.nil? #=> true ...
- version can neither be null, empty nor blank
在用mybatis-generator逆向生成mapper和DAO的时候,出现了这个错误. mybatis-generator:generate 原因是在pom.xml中我的mysql依赖没有写版本号 ...
- ruby基本语法(2)
关于数组 Ruby数组中的数据类型可以不相同并且长度也是可变的.(好聪明啊感觉用的久了就会变笨了,除非你本来就是老手)比如下面的例子 Myarray=[1,2,“ruby”] Ruby也支持那种-1的 ...
- 如何选择前端框架:ANGULAR VS EMBER VS REACT
最近一段时间是令前端工程师们非常兴奋的时期,因为三大Web框架陆续发布新版本,让我们见识到了更强大的Web框架.Ember2.0在2个月之前已经发布,从1.0升级到2.0非常简单.几周之前React发 ...
随机推荐
- ubuntu下修改进入root用户和修改文件权限
(1)进入root用户 su root 密码:设置的root密码 (2)修改文件权限 sudo chmod +777 file (3)执行shell ./shellfile (4)编写shell 第 ...
- tomcat中catalina是什么(转)
转自http://blog.sina.com.cn/s/blog_700aa8830101kgbk.html 谢谢博主的总结 catalina 就是Tomcat服务器使用的 Apache实现的serv ...
- 面向小白的JS笔记 - #Codecademy#学习笔记
前言 最初浏览过<JavaScript秘密花园>,前一段时间读过一点点<JavaScript语言精粹>和一点点<JavaScript高级程序设计>(一点点是指都只是 ...
- Win10/UWP开发—凭据保险箱PasswordVault
PasswordVault用户凭据保险箱其实并不算是Win10的新功能,早在Windows 8.0时代就已经存在了,本文仅仅是介绍在UWP应用中如何使用凭据保险箱进行安全存储和检索用户凭据. 那么什么 ...
- OGRE的学习资源
本文介绍从哪儿开始学习OGRE(Object-Oriented Graphics Rendering Engine的简称,又叫做OGRE 3D),如何在网上找寻OGRE的学习资源. 首先是wikipe ...
- mongodb字段类型转化
最近在使用mongoDB, 发现mongo对字段类型的定义并不是很严格,完全依赖传入数据的类型,在加上PHP是弱类型的语言,所以难免会出现一些错误.如果预想的类型是Int型,但数据存储的是String ...
- 【php学习】PHP 入门经典第二章笔记
问题答疑: 1.默认情况下,Apache服务器的配置文件名.MySQL服务器的配置文件名以及PHP预处理器配置文件名分别是什么?Apache默认主配置文件:根目录下config文件夹下httpd.co ...
- 迅达云s3cmd客户端mac平台部署说明
自己根据文档整理了下,在这里记下,免得其他兄弟走弯路. 1 下载最新的s3cmd代码 https://github.com/s3tools/s3cmd/archive/master.zip 2 解压缩 ...
- PHP character garbled
MySql 控制台查询时出现乱码 Database&Table 的字符集 于Mysql控制台显示的字符集不一样 右键单击mysql控制台边框 单击属性 查看当前代码页的字符集模式是否于数 ...
- 修改phpcms会员登录后头部登陆条的会员名称不带括号
phpcms会员登录后显示会员名称是带括号的,现在把他修改成不带括号. 找到函数库libs/functions/global.func.php,修改如下即可: function get_nicknam ...