.nil?

  • Ruby方法

  • .nil?方法被放置在Object类中,可以被任何对象调用,如果是nil则返回true

  • 在Rails中只有nil对象才会返回true

nil.nil? #=> true
anthing_else.nil? #=> false
a = nil
a.nil? #=> true
"".nil #=> true

.empty?

  • Ruby方法

  • 如果对集合之外的对象调用.empty?方法,则会抛出NoMethodError

  • 可以在字符串、数组、hash这几种集合中使用,且只有当集合长度(length)为0时才会返回true

"".empty = true
" ".empty? = false

.blank?

  • Rails方法

  • .empty?的区别: 对于还有空格的字符串也会返回true,并且nil返回的结果也未true

  • 可以在任意对象上调用,不限制于集合对象

nil.blank? = true
[].blank? = true
{}.blank? = true
"".blank? = true
5.blank? == false

.present?

  • Rails方法

  • 如果一个对象不是blank,即会返回true

  • 内部实现其实是调用的·blank?,如下代码:

# File activesupport/lib/active_support/core_ext/object/blank.rb, line 22
def present?
!blank?
ends

 

Rails中nil? empty? blank? present?的区别的更多相关文章

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

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

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

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

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

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

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

  5. ruby中nil?, empty? and blank?

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

  6. el中保留字empty与null的区别

    先看例子: <%@page pageEncoding="utf-8" %><BR>name:${param.name }<br /> empty ...

  7. 在ios开发中nil和NUll和Nilde区别————和如何判断连个对象的关系和UISlider不能拖动的问题

    nil表示一个对象指针为空,针对对象 >示例代码: NSString *someString = nil; NSURL *someURL = nil; id someObject = nil; ...

  8. 说一说Jquery中的empty()与remove()的区别和用法

    今天在改一个新人写的代码,好吧,我承认改代码比自己写代码要来的痛苦. 主要是在测试一个table.table中有一列是删除操作.我的删除功能是 这样的,当点击删除的时候,判断这个table中的tr行是 ...

  9. php中is_null,empty,isset,unset 的区别详细介绍

    is_null, empty, isset, unset 我们先来看看这4个函数的描述. isset 判断变量是否已存在(配置)unset 把变量删除(释放)掉empty 判断变量是否为空is_nul ...

随机推荐

  1. github的使用简易教程

    一.安装git https://git-for-windows.github.io/ git  ->  git bash 二.配置参数 $ git config --global user.na ...

  2. 洛谷noip 模拟赛 day1 T1

    T7925 剪纸 题目描述 小芳有一张nnn*mmm的长方形纸片.每次小芳将会从这个纸片里面剪去一个最大的正方形纸片,直到全部剪完(剩下一个正方形)为止. 小芳总共能得到多少片正方形纸片? 输入输出格 ...

  3. 动画-CAAnimationGroup(动画组合)

    动画-CAAnimationGroup(动画组合) 我们知道IOS可以完成多种类型的动画,但是如果我们想在同一个时间端内同事完成两种或者两种以上的动画组合的时候是不是可以呢?答案是肯定的. 这里我们有 ...

  4. 免费的二维码发布平台 http://zhifubao.masao.top:8282/assets/index.html

    http://zhifubao.masao.top:8282/assets/index.html

  5. [LeetCode] Search for a Range 二分搜索

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  6. python中函数和生成器的运行原理

    #!/usr/bin/env python # -*- coding:utf-8 -*- # author:love_cat # python的函数是如何工作的 # 比方说我们定义了两个函数 def ...

  7. 同样的promise,调用方法不一样,执行顺序不一样

    在测试两个不同的代码时发现的. 其实也理解,一个是新建一个空的promise,另一个是新建的promise马上执行,去分支resolve状态, 当然就空的在后. <script> //se ...

  8. POJ 3080-Blue Jeans【kmp,字符串剪接】

    Blue Jeans Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20695   Accepted: 9167 Descr ...

  9. Codeforces 1025D Recovering BST

    这个题被wa成傻逼了.... ma[i][j]表示i,j能不能形成一条直接作为排序二叉树的边,n^3更新维护ma即可,按说应该是要爆复杂度的,数据玄学吧.. #include<iostream& ...

  10. AppScan入门工作原理详解

    AppScan,即 AppScan standard edition.其安装在 Windows 操作系统上,可以对网站等 Web 应用进行自动化的应用安全扫描和测试. Rational AppScan ...