.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 any one from the above list. But one needs to be careful in using them and it is better that we understand it well before using it. Let's see which method does what. .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
.nil? .empty? .blank? .present? in Ruby on Rails的更多相关文章
- 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 ...
- 11月28日 记录一个错误❌,看ruby on rails --active support core extensions--present? && presence && duplicable?
❌错误 1. @job.resume.count: 提示❌ undefined method `resume' ✅: @job.resumes.count //解释:调出某一个job的所有简历, ...
- [ruby on rails] 跟我学之(3)基于rails console的查增删改操作
本章节展开对model的介绍:包括查增删改操作.紧接着上面一节<[ruby on rails] 跟我学之HelloWorld> 创建模型 使用命令创建模型 创建表post,默认自带两栏位 ...
- 自己总结的ruby on rails 查询方法
闲来无事,结合以前的代码,总结了ruby on rails的查询方法,方便自己以后查看,也方便后来人,如下,欢迎批评指正 1::simpleDB modules = find(:all, :condi ...
- Ruby on Rails 實戰聖經阅读(二)
1.操作系统 centos5.4 2.安装ruby yum install ruby 会安装得到 1.8.5 如果你公司用的是1.8.X就无所谓了, 拿这个学习就行了 如果你们公司用的是1.9.X,那 ...
- 【转】Ruby on Rails中select使用方法
在Ruby on Rails中真的有一堆Select helper可以用,我们经常容易混淆.常见的有三个..select, select_tag, collection_select(其余的什么sel ...
- empty blank
非nil对象才能调用 empty nil: 对象是否存在empty: ”“ []blank: nil emptypresent: ! blank
- Ruby on Rails 单元测试
Ruby on Rails 单元测试 为什么要写测试文件? 软件开发中,一个重要的环节就是编写测试文件,对代码进行单元测试,确保程序各部分功能执行正确.但是,这一环节很容易被我们轻视,认为进行单元测试 ...
随机推荐
- AndroidStudio导入项目常见问题
问题一: 解决:少了依赖的路径 问题二: 解决:把runProguard 改成minfyEnable 来源:http://blog.csdn.net/pengkv/article/details/44 ...
- Quarzt.NET 任务调度框架
Quartz.NET是一个开源的作业调度框架,是 OpenSymphony 的 Quartz API 的.NET移植,它用C#写成,可用于winform和asp.net应用中.它提供了巨大的灵活性 ...
- Open vSwitch
https://github.com/openvswitch/ovs/blob/master/INSTALL.RHEL.md
- 九度OJ 1010:计算A+B【字符串和数组】
/*======================================================================== 题目1010:A + B 时间限制:1 秒内存限制 ...
- 前端优化:DNS预解析提升页面速度
在网页体验中我们常会遇到这种情况,即在调用百度联盟.谷歌联盟以及当前网页所在域名外的域名文件时会遇到请求延时非常严重的情况.那么有没有方法去解决这种请求严重延时的现象呢? 一般来说这种延时的原因不会是 ...
- Oboe 提升web 用户体验以及性能
Oboe 地址:http://oboejs.com/ 1.安装 bower bower install oboe 2.使用,ajax 模式 oboe('/myapp/things.json') ...
- Web版报表的架构和设计思路
通常,我们设计一个报表,会面临设计报表,构造数据,展示报表的一个过程 设计报表也许大家都会用过,比如水晶报表,jreport一堆一堆的. 不过我觉得最好用的应该还是office做成的报表,谁都会,而且 ...
- HackerRank "No Prefix Set"
Typical Trie usage. But please note that it could be any order of input strings. #include <algori ...
- 更改AD查询LDAP条目的1000限制
解除LDAP导入时的AD条目查询限制 解除LDAP导入或读取AD用户数限制问题 更改AD查询LDAP条目的1000限制 来源:http://www.jiancool.com/article/55373 ...
- CSS hack样式兼容模式收藏
part1 —— 浏览器测试仪器,测试您现在使用的浏览器类型 IE6 IE7 IE8 Firefox Opera Safari (Chrome) IE6 IE7 IE8 ...