Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Add or_if_blank to Object

#1815

A utility method so that instead of having to write:

self.association_proxy.email.blank? ? self.name : self.association_proxy.email

all over the place, you can just write:

self.association_proxy.email.or_if_blank self.name

Works wherever blank? is defined.

Discussed on this thread: http://groups.google.com/group/r...

Reported by Ryan Angilly · January 29th, 2009 @ 05:29 AM

State: wontfix
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. Aliaksey Kandratsenka
    Aliaksey Kandratsenka

    I'm not sure if its good idea, because there's a significant difference with '||'. Usual 'or' is short-circuiting, while this method cannot work that way.

    I think that this should at least be noted in the documentation of this method, so that people don't try things like '.or_if_blank? raise', or '.or_if_blank? <do_something_else_expensive>'

    And BTW some time ago my co-worked showed me some blog post where some dude proposed '|' for same operation. You might want to consider this syntax, although I think that will be worse, because of the above mentioned non-short-circuiting behavior.

    January 29th, 2009 @ 06:43 AM

  2. Matt Jones
    Matt Jones

    The only way I could see to get short-circuit behavior would be to pass a block; it would look kind of weird:

    object.param.or_if_blank { object.other_param }

    Just a thought.

    January 29th, 2009 @ 07:01 AM

  3. Matt Jones
    Matt Jones

    Had a thought just after posting - it wouldn't be as generic as the above, but what about an operator like or ?

    So the above would be:

    
    object.param._or_.other_param
    

    There'd be some interesting trickery needed (see the implementation of SafeNil for ideas), but it would be pretty clear. Of course, I'm a little biased as I've gotten spoiled by hobosupport's _? operator (equivalent to try).

    January 29th, 2009 @ 07:07 AM

  4. Josh Susser
    Josh Susser

    I was going to suggest the block as an option. It's easy to do:

    def or_if_blank(other=nil) self.blank? ? (other || yield) : self end

    Then you can call it with either an immediate value or a block to get the value with a deferred computation.

    person.nickname.or_if_blank(person.full_name)

    person.nickname.or_if_blank { person.full_name }

    January 29th, 2009 @ 07:40 AM

  5. tf
    tf

    I really like the idea of adding lazy evaluation by passing a block. A little note in the docs can point out the difference between "? :" and or_if_blank.

    January 29th, 2009 @ 12:16 PM

  6. MatthewRudy
    MatthewRudy

    Interesting.

    But I like my BumbleBee approach, which is inline with how :try is implemented

    
    person.try(:nickname) || person.full_name # if you care about a :nil? nickname
    person.bumblebee(:nickname, :full_name) # if you care about a :blank? nickname
    

    or with fuller defaulting

    
    person.bumblebee(:nickname, :full_name) || "unknown"
    

    My full gist on the matter is here; http://gist.github.com/54501

    January 29th, 2009 @ 12:16 PM

  7. MatthewRudy
    MatthewRudy

    (notably the bumblebee doesn't know anything about args or blocks, which :try does)

    January 29th, 2009 @ 12:17 PM

  8. tf
    tf

    The bumblebee is kind of cool. Still it does not cover slightly more complex cases:

    
    person.hometown.or_if_blank { person.address.town }
    

    Silly example. You get the idea.

    January 29th, 2009 @ 12:33 PM

  9. Ryan Angilly
    Ryan Angilly

    Yeah I really like the block idea. Patch updated.

    January 29th, 2009 @ 01:06 PM

  10. Pratik
    Pratik
    • State changed from new to wontfix
    • Title changed from [PATCH] Add or_if_blank to Object to Add or_if_blank to Object

    I don't really think we need to add any of these methods. try() is for a common pattern inside the views. or_if_blank seems like code smell.

    January 29th, 2009 @ 06:16 PM

  11. Ryan Angilly
    Ryan Angilly

    try() doesn't help with blank strings, which was the whole point of what I was suggesting.

    And out of curiosity, what's the relevance of try()'s usage in views? I use my own version of or_if_blank in models, controllers, and helpers just as much as in views.

    January 30th, 2009 @ 02:31 AM

  12. tf
    tf

    If this functionality is not wanted - for whatever reason - I'd consider packaging it up as a gem. Still I have to agree that I do not see how try could be of any help in this use case.

    January 30th, 2009 @ 11:41 AM