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? chec ...
- 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?的选择
article = nil article.nil? # => true empty? checks if an element - like a string or an array f.e. ...
- Rails中nil? empty? blank? present?的区别
.nil? Ruby方法 .nil?方法被放置在Object类中,可以被任何对象调用,如果是nil则返回true 在Rails中只有nil对象才会返回true nil.nil? #=> true ...
- .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中symbol
Symbol 是什么 Ruby 是一个强大的面向对象脚本语言(本文所用 Ruby 版本为1.8.6),在 Ruby 中 Symbol 表示“名字”,比如字符串的名字,标识符的名字. 创建一个 Symb ...
- 在 Ruby 中执行 Shell 命令的 6 种方法
我们时常会与操作系统交互或在 Ruby 中执行 Shell 命令.Ruby为我们提供了完成该任务的诸多方法. Exec Kernel#exec 通过执行给定的命令来替换当前进程,例如: $ irb & ...
- ruby中的可调用对象--proc和lamdba
ruby中将块转变成对象的三种方法 ruby中的大部分东西都是对象,但是块不是.那么,如果你想存下来一个块,方便以后使用,你就需要一个对象.ruby中有三种方法,把块转换成可以利用的对象. Proc. ...
随机推荐
- item style edit in sharepoint 2013
标题头添加属性:(如果需要使用ddwrt)xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime&quo ...
- 协程IO多路复用
协程:单线程下实现并发并发:伪并行,遇到IO就切换,单核下多个任务之间切换执行,给你的效果就是貌似你的几个程序在同时执行.提高效率任务切换 + 保存状态并行:多核cpu,真正的同时执行串行:一个任务执 ...
- udid iphone6 获取
http://www.udidregistration.org/how-to-find-udid-of-iphone-6.html
- PyMysql复习
参考:http://www.cnblogs.com/liwenzhou/p/8032238.html 使用pycharm操作数据库. 填一个数据库名,User:填root 填写要连接的数据库. 建表. ...
- 【JavaScript】 js立即执行函数
( function(){…} )()和( function (){…} () )是两种javascript立即执行函数的常见写法,一般理解是一个括号包裹匿名函数,再在后面加个括号调用函数,最后达到函 ...
- vue 学前班003(生命周期)
ue把整个生命周期划分为创建.挂载.更新.销毁等阶段,每个阶段都会给一些“钩子”让我们来做一些我们想实现的动作.学习实例的生命周期,能帮助我们理解vue实例的运作机制,更好地合理利用各个钩子来完成我们 ...
- node 无脑生成小程序二维码图
RT 新建createwxaqrcode.js: const request = require('request') const fs = require('fs') // eg:生成购物车列表圆形 ...
- java中的安全模型(沙箱机制)
java中的安全模型(沙箱机制) java安全沙箱(一)之ClassLoader双亲委派机制 java安全沙箱(二)之.class文件检验器 java安全沙箱(三)之内置于Java虚拟机(及语言)的安 ...
- 用 Python+nginx+django 打造在线家庭影院
用 Python+nginx+django 打造在线家庭影院 2018年11月29日 08:46:59 清如許 阅读数:1528 我喜欢看电影,尤其是好的电影,我会看上三四遍,仔细感受电影带给我的 ...
- WebDriverAPI(9)
操作JavaScript的Alert窗口 测试网址代码 <html> <head> <title>你喜欢的水果</title> </head> ...