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

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?" - RailsAPI

nil.nil? = trueanthing_else.nil? = falsea = nila.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
时间: 2024-10-24 14:06:31

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? checks if an element - like a string or an array f.e. - is empty: # Array [].empty? #=> true # String "".empty? #=> true Rails adds the method 

ruby nil? empty? blank? 的区别

ruby nil? empty? blank? 一句话区别: nil?与empty? 除了对象用nil?其他用empty?判断 ,blank?的用法几乎是前两者的结合体 nil?用于对象 object sky = nil sky.nil? # => true其他的对象的都为 object.nil? 都为false 如数据库的一个属性为空,则 属性.nil? # => true empty? 用于string 和 array 还有hash # Array [].empty? #=> tru

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

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

.nil? , .empty?, .blank? .present? 的区别 首先这三个都是判空的. 而 .nil? 和 .empty? 是ruby的方法. .blank? 是rails的方法 .nil?       判断对象是否存在(nil).不存在的对象都是nil的 .empty?  对象已经存在,判断是否为空字段,比如一个字符串是否为空串,或者一个数组中是否有值.有点像判断长度是否为零,呵呵 .blank?   相当于同时满足 .nil? 和 .empty? .railsAPI中的解释是如

Ruby 判断是否空 empty? nil?

empty? 对象的值是否为空 nil? 对象本身是否为空 rb(main):015:0> ''.empty? => true irb(main):016:0> ' '.empty? => false irb(main):017:0> "".empty? => true irb(main):018:0> " ".empty? => false irb(main):019:0> nil.empty? NoMetho

ruby基本语法(2)

关于数组 Ruby数组中的数据类型可以不相同并且长度也是可变的.(好聪明啊感觉用的久了就会变笨了,除非你本来就是老手)比如下面的例子 Myarray=[1,2,“ruby”] Ruby也支持那种-1的表示,比如 arry=[1,2,"ruby"] puts arry[-4] puts arry[-3] puts arry[-2] puts arry[-1] puts arry[0] puts arry[1] puts arry[2] puts arry[3] 输出: 1 2 ruby

<Ruby> Basics

1. puts "Hello" This write "Hello" to the screen with a new line tailed to. print "Hello" Just like puts, but without new line. 2. "You know nothing".length This output the string length. "Jon Snow".revers

java代码测试---插入排序和选择排序

1 public class QuickSort { 2 3 //插入排序 4 //插入前的序列是排序好的,将新插入的数值与之前的数值比较 5 //直到找到合适的位置 6 public static int[] quickSort(int[] arr){ 7 8 for(int j=1;j<arr.length;j++){ 9 int key = arr[j]; 10 int i = j-1; 11 12 while(i>=0 && arr[i]<key){ 13 arr

如何从 0 开始学 ruby on rails (漫步版)

如何从 0 开始学 ruby on rails (漫步版) ruby 是一门编程语言,ruby on rails 是 ruby 的一个 web 框架,简称 rails. 有很多人对  rails 感兴趣,但又不知道从何下手.学习路线是什么,因为在多个场合下回答过类似问题,所以决定整理成文章供大家参观. 有很多人选择直接学习 rails,在学习使用 rails 的过程中学习 ruby.但我觉得这有些本末倒置,我更推崇先学 ruby 再学 rails,在对 ruby 有了一定的了解后再学 rails