Combine @item.foo.nil? || @item.foo.empty?

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

···

--
Posted via http://www.ruby-forum.com/.

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

tr{ tr @item.foo } unless @item.foo.to_s.empty?

-a

···

On Sat, 9 Sep 2006, Joe Ruby wrote:

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

--
what science finds to be nonexistent, we must accept as nonexistent; but what
science merely does not find is a completely different matter... it is quite
clear that there are many, many mysterious things.
- h.h. the 14th dalai lama

tr do
  td @item.foo
end unless Array(@item).empty?

···

On 08/09/06, Joe Ruby <joeat303@yahoo.com> wrote:

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

--
Posted via http://www.ruby-forum.com/\.

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/8/06, Joe Ruby joeat303@yahoo.com wrote:

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

Matthew Moss 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.

···

--
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

ara.t.howard@noaa.gov 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

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, Marshall T. Vandegrift wrote:

ara.t.howard@noaa.gov 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

--
what science finds to be nonexistent, we must accept as nonexistent; but what
science merely does not find is a completely different matter... it is quite
clear that there are many, many mysterious things.
- h.h. the 14th dalai lama

ara.t.howard@noaa.gov 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

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 Sep 9, 2006, at 10:37 AM, Marshall T. Vandegrift wrote:

So why not just do

    class NilClass
      def empty?; true; end
    end

?

-Marshall

So define as follows:

class NilClass
  def nil_or_empty?
    true
  end
end

···

On Sat, 09 Sep 2006 06:53:59 +0900, Joel VanderWerf wrote:

Matthew Moss 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.

--
Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/

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

···

On 9/9/06, Marshall T. Vandegrift <llasram@gmail.com> wrote:

    class NilClass
      def empty?; true; end
    end

--
Austin Ziegler * halostatue@gmail.com * http://www.halostatue.ca/
               * austin@halostatue.ca * You are in a maze of twisty little passages, all alike. // halo • statue
               * austin@zieglers.ca