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

Discussion in 'Ruby' started by Joe Ruby, Sep 8, 2006.

  1. Joe Ruby

    Joe Ruby Guest

    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/.
    Joe Ruby, Sep 8, 2006
    #1
    1. Advertising

  2. Joe Ruby

    Matthew Moss Guest

    > 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 Moss, Sep 8, 2006
    #2
    1. Advertising

  3. 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
    Joel VanderWerf, Sep 8, 2006
    #3
  4. Joe Ruby

    Guest

    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


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

    -a
    --
    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
    , Sep 8, 2006
    #4
  5. Joe Ruby

    Guest

    On Sat, 9 Sep 2006, Marshall T. Vandegrift wrote:

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

    > 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.)

    > So why not just do
    >
    > class NilClass
    > def empty?; true; end
    > end
    >
    > ?
    >
    > -Marshall
    >
    >
    Logan Capaldo, Sep 9, 2006
    #6
  7. Joe Ruby

    Ken Bloom Guest

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


    So define as follows:

    class NilClass
    def nil_or_empty?
    true
    end
    end

    --
    Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.
    Department of Computer Science. Illinois Institute of Technology.
    http://www.iit.edu/~kbloom1/
    Ken Bloom, Sep 10, 2006
    #7
  8. On 08/09/06, 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
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >


    tr do
    td @item.foo
    end unless Array(@item).empty?
    Farrel Lifson, Sep 10, 2006
    #8
  9. On 9/9/06, Marshall T. Vandegrift <> 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
    --
    Austin Ziegler * * http://www.halostatue.ca/
    * * http://www.halostatue.ca/feed/
    *
    Austin Ziegler, Sep 11, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    383
    John Roth
    Jul 29, 2005
  2. .rhavin grobert

    vector: Foo[5] == ((foo*)Foo) + 5 ?

    .rhavin grobert, Sep 23, 2008, in forum: C++
    Replies:
    4
    Views:
    390
    JaredGrubb
    Sep 24, 2008
  3. Brian Candler

    puts nil generates "nil\n"

    Brian Candler, Nov 6, 2004, in forum: Ruby
    Replies:
    1
    Views:
    111
  4. Christoffer Sawicki
    Replies:
    5
    Views:
    253
    Christoffer Sawicki
    Sep 2, 2006
  5. Replies:
    4
    Views:
    142
    Thomas 'PointedEars' Lahn
    Dec 23, 2007
Loading...

Share This Page