I have code like this in my (Markaby) templates:
tr do
td @item.foo
end unless (@item.foo.nil? || @item.foo.empty?)
Is there a way to combine these two OR clauses into one?
tr do
td @item.foo
end unless @item.foo.nil_or_empty?
Thanks,
Joe
I have code like this in my (Markaby) templates:
tr do
td @item.foo
end unless (@item.foo.nil? || @item.foo.empty?)
Is there a way to combine these two OR clauses into one?
tr do
td @item.foo
end unless @item.foo.nil_or_empty?
Thanks,
Joe
tr do
td @item.foo
end unless (@item.foo.nil? || @item.foo.empty?)Is there a way to combine these two OR clauses into one?
Define a method on the class of foo?
class Foo
def nil_or_empty?
return nil? || empty?
end
end
Matthew M. wrote:
tr do
td @item.foo
end unless (@item.foo.nil? || @item.foo.empty?)Is there a way to combine these two OR clauses into one?
Define a method on the class of foo?
class Foo
^^^
Object
def nil_or_empty?
return nil? || empty?
end
end
Otherwise it won’t be defined when foo==nil.
[email protected] writes:
tr{ tr @item.foo } unless @item.foo.to_s.empty?
It makes sense to me that
object.to_s.empty? # => true
should imply
object.empty? # => true
So why not just do
class NilClass
def empty?; true; end
end
?
-Marshall
On Sat, 9 Sep 2006, Marshall T. Vandegrift wrote:
object.empty? # => true
So why not just do
class NilClass
def empty?; true; end
end?
-Marshall
def deep_in_some_other_unrelated_code
begin
should_always_be_empty = buggy_method
abort unless should_always_be_empty.empty? # OOPS!
transfer_four_million_euros_to_swiss_account
rescue
hours_of_debugging?
ensure
modify_builtins_with_care!
end
end
-a
On Sat, 9 Sep 2006, Joe R. wrote:
end unless @item.foo.nil_or_empty?
Thanks,
Joe
tr{ tr @item.foo } unless @item.foo.to_s.empty?
-a
On Sep 9, 2006, at 10:37 AM, Marshall T. Vandegrift wrote:
object.empty? # => true
Doesn’t to me. nil is not a container, and it should not be treated
as one. A string is a container (contains a sequence of characters)
and can therefore be empty. The fact that one, arbitrary string
representation of nil happens to be the empty string doesn’t mean
that nil is empty. (This is also why I disklike rails’s #blank?, but
at least it’s named something different.)
On Sat, 09 Sep 2006 06:53:59 +0900, Joel VanderWerf wrote:
class Foo
^^^
Object
def nil_or_empty?
return nil? || empty?
end
endOtherwise it won’t be defined when foo==nil.
So define as follows:
class NilClass
def nil_or_empty?
true
end
end
On 08/09/06, Joe R. [email protected] wrote:
end unless @item.foo.nil_or_empty?
Thanks,
Joe–
Posted via http://www.ruby-forum.com/.
tr do
td @item.foo
end unless Array(@item).empty?
On 9/8/06, Joe R. [email protected] wrote:
td @item.foo
end unless @item.foo.nil_or_empty?Thanks,
Joe
Perhaps #blank? from activesupport [1] could do what you want?
-Scott
[1]
http://dev.rubyonrails.org/browser/tags/rel_1-1-6/activesupport/lib/active_support/core_ext/blank.rb
On 9/9/06, Marshall T. Vandegrift [email protected] wrote:
class NilClass def empty?; true; end end
Because nil isn’t empty.
It isn’t a container. It’s not an array, hash, or string.
Rails has a #blank? method – which I’ve used this weekend – but
nil.empty? is a false question.
-austin
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs