`
clark1231
  • 浏览: 247674 次
  • 性别: 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的空节点添加

    c#设置xml内容不换行:添加属性为xsi:nil=true的空节点便可实现,感兴趣的你可以参考下本文,或许有意想不到的收获

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

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

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

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

    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

    ios-清理模型中的nil.zip

    清理字典转模型后模型中的nil,属性多于字典,服务器返回为null等情况

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

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

    对Golang的nil的理解和认识

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

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

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

    lua-cjson decode中null改成nil

    lua-cjson lua的cjson库 decode略做修改 结果中null还原成nil

    CE中文版-启点CE过NP中文.exe

    Changed the processlist and added an Applications view similar to the taskmanager Small change to the tutorial first step wording Structure Dissect: Added RLE compression (by mgr.inz.player) and other...

    pattern matcher

    It returns a list of variable-value bindings if the facts match the patterns while it gives nil if the match fails. The syntax for facts can be any valid structure in Lisp. Also, patterns can include...

Global site tag (gtag.js) - Google Analytics