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…
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 …
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…
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…
.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 可以在字符串.数组.ha…
We get confused when there are many options to choose from. Same is the case when it comes to use any one from the above list. But one needs to be careful in using them and it is better that we understand it well before using it. Let's see which meth…
.nil? , .empty?, .blank? .present? 的区别 首先这三个都是判空的. 而 .nil? 和 .empty? 是ruby的方法. .blank? 是rails的方法 .nil?       判断对象是否存在(nil).不存在的对象都是nil的 .empty?  对象已经存在,判断是否为空字段,比如一个字符串是否为空串,或者一个数组中是否有值.有点像判断长度是否为零,呵呵 .blank?   相当于同时满足 .nil? 和 .empty? .railsAPI中的解释是如…
我们时常会与操作系统交互或在 Ruby 中执行 Shell 命令.Ruby为我们提供了完成该任务的诸多方法. Exec Kernel#exec 通过执行给定的命令来替换当前进程,例如: $ irb >> exec 'echo "hello $HOSTNAME"' hello codefun $ 注意 exec 利用 echo 命令替换了 irb 进程,然后退出.因为 Ruby 实际上结束了该方法,所以只能有限使用.该方法的缺点是,你无法从 Ruby 脚本中知道命令是执行成功…
原文出处:Understanding Ruby Blocks, Procs and Lambdas blocks,Procs和lambda(在编程领域被称为闭包)是Ruby中很强大的特性,也是最容易引起误解的特性. 这有可能是因为Ruby使用相当独特的方式来处理闭包.Ruby有四种处理闭包的方式,每一种方式都稍有点不同,甚至有点荒诞,这使得事情变得有点复杂.有不少网站提供了一些关于Ruby闭包的工作方式,但是我还没有找到一个非常有效的指南,希望本篇文章会成为这样的一篇指南. 一.首先来说bloc…
作用域对于Ruby以及其它编程语言都是一个需要理解的至关重要的基础知识.在我刚开始学习ruby的时候遇到很多诸如变量未定义.变量没有正确赋值之类的问题,归根结底是因为自己对于ruby作用域的了解不够,但在你看看完我的这篇文章后,相信你不会再担心会遇到这些头疼的问题. 什么是作用域? 当谈论到作用域的时候,应该马上想到变量和可见性这两个词,变量和可见性是作用域的主要内容,没错,作用域就是关于在代码的什么地方什么变量是可见的,当你充分了解了作用域后,给你一段代码,你可以轻易知道此时什么变量是可见的,…