return x.nil? or blank?
--
Posted via http://www.ruby-forum.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?
(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
def blank?
if respond_to? (:empty?) && respond_to?(:strip )
empty? or strip.empty?
elsif respond_to?(:empty?)
empty?
else
! self
end
end
> 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
x.blank? is equivalent to x.nil? or x.empty?
www.webdevbros.net - ruby, rails & co.