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?" - RailsAPInil.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作用域的了解不够, ...
随机推荐
- JAVA获取Spring上下文
1. 添加监听 public class SpringContextListener implements ServletContextListener { //获取spring注入的bean对象 p ...
- How does asp.net web api work?
https://hub.packtpub.com/working-aspnet-web-api/ https://docs.microsoft.com/en-us/aspnet/web-api/ove ...
- java 命令行
javac 编译 linux平台下:javac -cp ./hadoop-common-2.7.1.jar:./hadoop-mapreduce-client-core-2.7.4.jar: Word ...
- LeetCode——max-points-on-a-line
Question Given n points on a 2D plane, find the maximum number of points that lie on the same straig ...
- Caused by: org.apache.ibatis.binding.BindingException: Parameter 'parameter' not found.解决
Caused by: org.apache.ibatis.binding.BindingException: Parameter 'company' not found. Available para ...
- Java -- JDBC 操作mysql数据库
1. Demo1 导包时 不要导具体的mysql包, 为了兼容性,导JDBC 中 sql的包既可以了. public class Demo1 { /** * @param args * @throws ...
- java创建多线程的三种方式
/***************************继承Thread类创建多线程************************/ public class FirstThread extends ...
- 语音01_TTS
1.http://blog.csdn.net/u010176014/article/details/47428595 2.家里 Win7x64 安装“微软TTS5.1语音引擎(中文).msi”之后,搜 ...
- 改善Chrome在Windows下的中文字体效果
方法一:摘自卡饭论坛 chrome://flags/#disable-direct-write 启用 chrome://flags/#num-raster-threads 改为 1 chrome:// ...
- rabbitmq_学习_01_rabbitmq安装
二.参考资料 1.RabbitMQ:windows10下安装 2.RabbitMQ系列(一):Windows下RabbitMQ安装及入门