`
clark1231
  • 浏览: 256518 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

the difference between nil, true, false, blank and empty

    博客分类:
  • ruby
阅读更多

The Nil Expression

It’s pretty important that you understand the difference of these expressions. To begin, let’s start with nil. Nil is the ruby way of saying NULL. Whenever there is an expression that returns nothing, we get a nil that specifies that. For instance, in a find_by, if the record is not there we get nil as a result. Notice that find returns RecordNotFound exception. As everything is an object in Ruby, Nil is also an object (of class NilClass). Nil is not the same as false.

False/True Expressions

In Ruby (and Rails of course), every expression that is not nil or false is considered true. False and True expressions work pretty much as expected, but pay attention to the fact that, again, they are objects (TrueClass, FalseClass). Take a look at the console below :

irb(main):053:0> 0==1
=> false
irb(main):054:0> 1==1
=> true
irb(main):055:0> nil==true
=> false
irb(main):056:0> nil==false
=> false
irb(main):057:0> 0==true
=> false
irb(main):058:0> 0==false
=> false
irb(main):060:0> true.class
=> TrueClass

 Nil is not either true or false. 0 is not either true or false(or nil). It’s just 0.

The Blank? Expression

Blank is a Rails expression that checks whether an object is either nil or false. Notice that blank evaluates correctly if the object is a space padded string.

irb(main):066:0> ''.blank?
=> true
irb(main):067:0> nil.blank?
=> true
irb(main):068:0> '     '.blank?
=> true
irb(main):069:0> 'a'.blank?
=> false
irb(main):070:0> false.blank?
=> true
irb(main):071:0> true.blank?
=> false
irb(main):073:0> [].blank?
=> true
irb(main):074:0> ['a'].blank?
=> false

 

 The Empty? Expression

 

Empty can only be used in some objects like an array or a string. As you can guess, it checks whether an object is empty (empty string, no elements array etc).

irb(main):078:0> '     '.empty?
=> false
irb(main):079:0> ''.empty?
=> true
irb(main):080:0> [].empty?
=> true
irb(main):081:0> ['a'].empty?
=> false
irb(main):082:0> nil.empty?
NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.empty?
 from (irb):82
 from :0

A space padded string is not empty, although it’s blank. Also notice that nil.empty? is an incorrect expression, because the nil object does not have an empty method.

分享到:
评论

相关推荐

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    - FIX: With little difference between curve angles at smaller scales the curve was drawn backwards (error in TFlexEllipse.Paint). - ADD: Support of the binary document format. Added the public ...

    Yaron Minsky, Anil Madhavapeddy, and Jason Hickey

    They affect the reliability, security, and efficiency of the code you write, as well as how easy it is to read, refactor, and extend. The languages you know can also change how you think, influencing...

    c#设置xml内容不换行及属性xsi:nil=true的空节点添加

    为了创建这样一个空节点,可以使用`XmlDocument`类创建一个新元素,并添加`xsi:nil`属性设置为`"true"`。代码示例如下: ```csharp public static XmlElement CreateNodeWithNullAttr(XmlDocument doc, string ...

    boolean2:Boolean2 是一个 Ruby 常量,它是 true 和 false 的祖先

    布尔值 2 下次要定义全局Boolean类之前,请考虑改用这种基本方法。 设置 添加到您的Gemfile : gem 'boolean2' 用法 true .... Boolean2 #=> true false .... Boolean2 #=> true ... new ( false ) #=> false

    lua-cjson decode中null改成nil

    `lua-cjson decode中null改成nil`这个话题,实际上是指在使用lua-cjson库进行JSON解码时,用户可能希望保留原始JSON数据中的`null`表示,而不是直接转换为`nil`。这可能是为了保持与JSON数据源的一致性,或者有特定...

    Chinese Entity Linking Comprehensive

    matching node for a query, the entity is marked as 'NIL' and then clustered with other NIL entities into equivalence classes. For more information, please refer to the Entity Linking section of NIST's...

    Data Analytics Made Accessible Maheshwari, Anil epub version

    This book fills the need for a concise and conversational book on the growing field of Data Analytics and Big Data. Easy to read and informative, this lucid book covers everything important, with ...

    ios-清理模型中的nil.zip

    标题"ios-清理模型中的nil.zip"所涉及的内容就是关于如何在模型转换过程中有效地处理这些`nil`或`null`值,以避免程序运行时出现异常。 描述中提到的"清理字典转模型后模型中的nil,属性多于字典,服务器返回为null...

    DevExpress VCL 13.2.5 D7-DXE6 FullSource

    •Q583166 - Restoring the tree list from an empty or corrupted store using RestoreFrom~ method calls assigns incorrect Coilumn.Position.BandIndex property values, and as a result, hides these columns ...

    WPTools.v6.29.1.Pro

    behaviour between ANSI and UNICODE version - when writing HTML background color is not set to white when shading is 0. 0 is treated as default value. - image align center and right now works in HTML ...

    Data Analytics Made Accessible Maheshwari, Anil pdf

    This book fills the need for a concise and conversational book on the growing field of Data Analytics and Big Data. Easy to read and informative, this lucid book covers everything important, with ...

    The Completed Guide to Understanding Swift Optional

    A detailed explanation of all those obscure terms you keep reading on the web, like unwrapping, optional binding, nil coalescing and guard statements. Never find yourself again not understanding the ...

    Ruby简明教程之判断语句介绍

    3) 有些方法虽然不会返回true或false,但是也能作为条件判断的条件,他们返回的对象,要么是false或nil,要么是意义的对象,则可根据如下表格判断: TRUE FALSE false和nil以外的对象 false与nil

    node-no-nil:从对象和数组中删除nil值。 将所有其他nil值转换为null

    没有零 从对象和数组中删除nil值。 将所有其他nil值转换为null。 例子 var noNil = require ( '...of this software and associated documentation files (the "Software"), to deal in the Software without restric

    Delphi7.1 Update

    * Setting MTSDataModule.AutoComplete to False in the IDE is overridden and set back to True at run time (Quality Central 4716).* Calls to ApplyUpdates(-1) on a TMTSDataModule do not work properly ...

    对Golang的nil的理解和认识

    布尔型零值为:false 整型零值为:0 字符串的零值为: 而指针,函数,interface,slice,channel,map的零值均为nil Go文档中说明:nil是预定义的标识符,代表指针,通道,函数,接口,映射或者切片的零值,并不是Go...

    Common Lisp The Language Second Edition

    - **1.2.2 Nil, False, and the Empty List NIL、FALSE 和空列表**:解释了这些概念在Common Lisp中的含义及其用途。 - **1.2.3 Evaluation, Expansion, and Equivalence 评估、扩展与等价性**:探讨了Common Lisp...

    persistent-soft:Emacs 的持久存储,失败时返回 nil

    概述Emacs 的持久存储,失败时返回 nil。快速开始( require 'persistent-soft )(persistent-soft-store 'hundred 100 " mydatastore " )(persistent-soft-fetch 'hundred " mydatastore " ) ; 100(persistent-soft-...

Global site tag (gtag.js) - Google Analytics