Rails中nil? empty? blank? present?的区别
.nil?
Ruby方法
.nil?方法被放置在Object类中,可以被任何对象调用,如果是nil则返回true在Rails中只有nil对象才会返回
true
nil.nil? #=> true
anthing_else.nil? #=> false
a = nil
a.nil? #=> true
"".nil #=> true
.empty?
Ruby方法
如果对集合之外的对象调用
.empty?方法,则会抛出NoMethodError可以在字符串、数组、hash这几种集合中使用,且只有当集合长度(length)为0时才会返回
true
"".empty = true
" ".empty? = false
.blank?
Rails方法
与
.empty?的区别: 对于还有空格的字符串也会返回true,并且nil返回的结果也未true可以在任意对象上调用,不限制于集合对象
nil.blank? = true
[].blank? = true
{}.blank? = true
"".blank? = true
5.blank? == false
.present?
Rails方法
如果一个对象不是
blank,即会返回true内部实现其实是调用的
·blank?,如下代码:
# File activesupport/lib/active_support/core_ext/object/blank.rb, line 22
def present?
!blank?
ends
Rails中nil? empty? blank? present?的区别的更多相关文章
- Rails :.nil? , .empty?, .blank? .present? 的区别
.nil? , .empty?, .blank? .present? 的区别 首先这三个都是判空的. 而 .nil? 和 .empty? 是ruby的方法. .blank? 是rails的方法 .ni ...
- .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 ...
- 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 ...
- el中保留字empty与null的区别
先看例子: <%@page pageEncoding="utf-8" %><BR>name:${param.name }<br /> empty ...
- 在ios开发中nil和NUll和Nilde区别————和如何判断连个对象的关系和UISlider不能拖动的问题
nil表示一个对象指针为空,针对对象 >示例代码: NSString *someString = nil; NSURL *someURL = nil; id someObject = nil; ...
- 说一说Jquery中的empty()与remove()的区别和用法
今天在改一个新人写的代码,好吧,我承认改代码比自己写代码要来的痛苦. 主要是在测试一个table.table中有一列是删除操作.我的删除功能是 这样的,当点击删除的时候,判断这个table中的tr行是 ...
- php中is_null,empty,isset,unset 的区别详细介绍
is_null, empty, isset, unset 我们先来看看这4个函数的描述. isset 判断变量是否已存在(配置)unset 把变量删除(释放)掉empty 判断变量是否为空is_nul ...
随机推荐
- Python之多线程:Threading模块
1.Threading模块提供的类 Thread,Lock,Rlock,Condition,Semaphore,Event,Timer,local 2.threading模块提供的常用的方法 (1)t ...
- 汕头市队赛 SRM 08 C
C-3 SRM 08 描述 给一个图,n 个点 m 条双向边,每条边有其长度.n 个点中有 k 个是特殊点,问任意两个特殊点的最短路是多少. 输入格式 第一行三个整数 n m k 第二行 k 个整数 ...
- masscan banners 不显示
https://github.com/robertdavidgraham/masscan/issues/221
- UVALIVE 2927 "Shortest" pair of paths
裸的费用流.一开始因为这句话还觉得要拆点 样例行不通不知道这句话干啥用的.Further, the company cannot place the two chemicals in same dep ...
- 使用clamav查杀病毒
cd ~ wget http://www.zlib.net/fossils/zlib-1.2.8.tar.gz .tar.gz cd zlib- ./configure --prefix=/usr/l ...
- l2tp连接不上,修复
启动依赖服务并设置为开机自启动 保存为.bat文件管理员权限执行 ::显示名称IPsec Policy Agent sc config "PolicyAgent" start= a ...
- 开始gentoo之旅
买了一台i7笔记本,dell 15游戏匣7559,老笔记本dell n4030装了gentoo. 用了两天,先用着,再慢慢学内核编译.
- css之其它技巧和经验列表
其它技巧和经验列表(*以下实例默认运行环境都为Standard mode): 如何让层在falsh上显示? 方法: ``` 设置flash的wmode值为transparent或opaque ``` ...
- FZU-2219 StarCraft(贪心)
Problem 2219 StarCraft Accept: 294 Submit: 860Time Limit: 1000 mSec Memory Limit : 32768 KB ...
- PMBOK及PMP考试精要
PROJECT MANAGEMENT KNOWLEDGE AREAS项目管理知识体系 2 PROJECT LIFE CYCLE / PROJECT MANAGEMENT PROCESS GROUPS项 ...