In Ruby, you check with nil? if an object is nil:

  1. article = nil
  2. article.nil? # => true

empty? checks if an element - like a string or an array f.e. - is empty:

  1. # Array
  2. [].empty? #=> true
  3. # String
  4. "".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

  1. if !address.nil? && !address.empty?

to

  1. if !address.blank?
  2. .nil?
  3. - 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
  4. nil.nil? = true
    anthing_else.nil? = false
    a = nil
    a.nil? = true
    “”.nil = false
  5. .empty?
  6. - 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

  1. - Running .empty? on something that is nil will throw a NoMethodError
  2. "".empty = true
    " ".empty? = false
  3. .blank?
  4. - It is Rails method
    - operate on any object as well as work like .empty? on strings, arrays and hashes.
  5. nil.blank? = true
    [].blank? = true
    {}.blank? = true
    "".blank? = true
    5.blank? == false
  6. - It also evaluates true on strings which are non-empty but contain only whitespace:
  7. "  ".blank? == true"  ".empty? == false
  8. Quick tip: !obj.blank? == obj.present?
  9. activesupport/lib/active_support/core_ext/object/blank.rb, line 17 # (Ruby 1.9)
  10. def present?
     !blank?
    end

ruby中nil?, empty? and blank?的更多相关文章

  1. ruby中nil?, empty? and blank?的选择

    In Ruby, you check with nil? if an object is nil: article = nil article.nil? # => true empty? che ...

  2. 【转】ruby中nil?, empty? and blank?的选择

    In Ruby, you check with nil? if an object is nil:article = nil article.nil? # => true empty? chec ...

  3. Rails中nil? empty? blank? present?的区别

    .nil? Ruby方法 .nil?方法被放置在Object类中,可以被任何对象调用,如果是nil则返回true 在Rails中只有nil对象才会返回true nil.nil? #=> true ...

  4. ruby : nil?, empty? and blank?的选择

    article = nil article.nil? # => true empty? checks if an element - like a string or an array f.e. ...

  5. .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 ...

  6. Rails :.nil? , .empty?, .blank? .present? 的区别

    .nil? , .empty?, .blank? .present? 的区别 首先这三个都是判空的. 而 .nil? 和 .empty? 是ruby的方法. .blank? 是rails的方法 .ni ...

  7. 在 Ruby 中执行 Shell 命令的 6 种方法

    我们时常会与操作系统交互或在 Ruby 中执行 Shell 命令.Ruby为我们提供了完成该任务的诸多方法. Exec Kernel#exec 通过执行给定的命令来替换当前进程,例如: $ irb & ...

  8. [翻译]理解Ruby中的blocks,Procs和lambda

    原文出处:Understanding Ruby Blocks, Procs and Lambdas blocks,Procs和lambda(在编程领域被称为闭包)是Ruby中很强大的特性,也是最容易引 ...

  9. 理解Ruby中的作用域

    作用域对于Ruby以及其它编程语言都是一个需要理解的至关重要的基础知识.在我刚开始学习ruby的时候遇到很多诸如变量未定义.变量没有正确赋值之类的问题,归根结底是因为自己对于ruby作用域的了解不够, ...

随机推荐

  1. JAVA获取Spring上下文

    1. 添加监听 public class SpringContextListener implements ServletContextListener { //获取spring注入的bean对象 p ...

  2. 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 ...

  3. java 命令行

    javac 编译 linux平台下:javac -cp ./hadoop-common-2.7.1.jar:./hadoop-mapreduce-client-core-2.7.4.jar: Word ...

  4. 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 ...

  5. Caused by: org.apache.ibatis.binding.BindingException: Parameter 'parameter' not found.解决

    Caused by: org.apache.ibatis.binding.BindingException: Parameter 'company' not found. Available para ...

  6. Java -- JDBC 操作mysql数据库

    1. Demo1 导包时 不要导具体的mysql包, 为了兼容性,导JDBC 中 sql的包既可以了. public class Demo1 { /** * @param args * @throws ...

  7. java创建多线程的三种方式

    /***************************继承Thread类创建多线程************************/ public class FirstThread extends ...

  8. 语音01_TTS

    1.http://blog.csdn.net/u010176014/article/details/47428595 2.家里 Win7x64 安装“微软TTS5.1语音引擎(中文).msi”之后,搜 ...

  9. 改善Chrome在Windows下的中文字体效果

    方法一:摘自卡饭论坛 chrome://flags/#disable-direct-write 启用 chrome://flags/#num-raster-threads 改为 1 chrome:// ...

  10. rabbitmq_学习_01_rabbitmq安装

    二.参考资料 1.RabbitMQ:windows10下安装 2.RabbitMQ系列(一):Windows下RabbitMQ安装及入门