x.nil? or x.blank?

60 views
Skip to first unread message

Michal Gabrukiewicz

unread,
Dec 6, 2007, 7:03:17 AM12/6/07
to rubyonra...@googlegroups.com
does someone knows if there is method which checks both? if not then
i'll write my own .. but i am curious if there is one for

return x.nil? or blank?
--
Posted via http://www.ruby-forum.com/.

Xavier Noria

unread,
Dec 6, 2007, 7:09:26 AM12/6/07
to rubyonra...@googlegroups.com
On Dec 6, 2007, at 1:03 PM, Michal Gabrukiewicz wrote:

> does someone knows if there is method which checks both? if not then
> i'll write my own .. but i am curious if there is one for
>
> return x.nil? or blank?

(x.nil? or x.blank?) is logically equivalent to x.blank? because nil
is blank:

nil.blank? # -> true

So the answer is yes, you are looking for blank? indeed :-).

-- fxn

Michal Gabrukiewicz

unread,
Dec 6, 2007, 7:13:10 AM12/6/07
to rubyonra...@googlegroups.com
oh thanks ... thats good to know ;)
great!

Robert Walker

unread,
Dec 7, 2007, 1:28:04 AM12/7/07
to Ruby on Rails: Talk
After a quick test it appears that Rails must add the blank? method to
nil. Ruby "out-of-the-box" reports that nil has no blank? method. Just
FYI.

On Dec 6, 7:13 am, Michal Gabrukiewicz <rails-mailing-l...@andreas-

Ryan Bigg

unread,
Dec 7, 2007, 1:34:29 AM12/7/07
to rubyonra...@googlegroups.com
According to:

http://noobkit.com/show/ruby/rails/rails-stable/activesupport/object/blank-3f.html

ActiveSupport is what includes the method.

All the code it uses is pretty simple:

def blank?
if respond_to? (:empty?) && respond_to?(:strip )
empty? or strip.empty?
elsif respond_to?(:empty?)
empty?
else
! self
end
end


eggie5

unread,
Dec 7, 2007, 2:20:14 AM12/7/07
to Ruby on Rails: Talk
I want one that checks for empty + empty array....

On Dec 6, 10:34 pm, "Ryan Bigg" <radarliste...@gmail.com> wrote:
> According to:
>
> http://noobkit.com/show/ruby/rails/rails-stable/activesupport/object/...
>
> ActiveSupport is what includes the method.
> I w
> All the code it uses is pretty simple:
>
> def blank?
> if respond_to?(:empty?) && respond_to?(:strip)
> empty? or strip.empty?
> elsif respond_to?(:empty?)
> empty?
> else
> !self
> end
> end
>

Xavier Noria

unread,
Dec 7, 2007, 5:07:00 AM12/7/07
to rubyonra...@googlegroups.com
On Dec 7, 2007, at 8:20 AM, eggie5 wrote:

> I want one that checks for empty + empty array....

That's blank? again.

In Rails blank? has an implementation in Object and a few special
cases. For example in Array

class Array #:nodoc:
alias_method :blank?, :empty?
end

In NilClass it returns true directly:

class NilClass #:nodoc:
def blank?
true
end
end

And there are a few more. See activesupport/lib/active_support/
core_ext/blank.rb.

-- fxn

Karthi kn

unread,
Dec 7, 2007, 6:56:11 AM12/7/07
to rubyonra...@googlegroups.com
Michal Gabrukiewicz wrote:
> does someone knows if there is method which checks both? if not then
> i'll write my own .. but i am curious if there is one for
>
> return x.nil? or blank?

x.blank? is equivalent to x.nil? or x.empty?

Michal Gabrukiewicz

unread,
Dec 7, 2007, 7:31:53 AM12/7/07
to rubyonra...@googlegroups.com
yeah as far as i see our guy is blank? ...

www.webdevbros.net - ruby, rails & co.

eggie5

unread,
Dec 7, 2007, 2:31:22 PM12/7/07
to Ruby on Rails: Talk
oh duh....
Reply all
Reply to author
Forward
0 new messages