Dealing with nil

Discussion in 'Ruby' started by Robert Klemme, Mar 3, 2008.

  1. Hi,

    we had frequent discussions about how to make code safely deal with
    possible nil values. I remember having seen various solutions
    proposed. However, I cannot remember having seen this, which just
    occurred to me:

    class Object
    def nil_safe(fallback = self, &b)
    if nil?
    fallback
    else
    instance_eval(&b)
    end
    end
    end

    With this, you can do

    irb(main):038:0> s=nil
    => nil
    irb(main):039:0> s.nil_safe(0) {length}
    => 0
    irb(main):040:0> s="foo"
    => "foo"
    irb(main):041:0> s.nil_safe(0) {length}
    => 3

    Admittedly this is not very pretty.

    An alternative definition would be

    class Object
    def nil_safe(fallback = self, &b)
    if nil?
    fallback
    else
    yield self
    end
    end
    end

    And then

    irb(main):051:0> s=nil
    => nil
    irb(main):052:0> s.nil_safe(0) {|x| x.length}
    => 0
    irb(main):053:0> s="foo"
    => "foo"
    irb(main):054:0> s.nil_safe(0) {|x| x.length}
    => 3

    What do others think?

    Kind regards

    robert

    --
    use.inject do |as, often| as.you_can - without end
     
    Robert Klemme, Mar 3, 2008
    #1
    1. Advertising

  2. Robert Klemme

    Paul McMahon Guest

    > With this, you can do
    >
    > irb(main):038:0> s=3Dnil
    > =3D> nil
    > irb(main):039:0> s.nil_safe(0) {length}
    > =3D> 0


    I don't really get your example...

    Why not just use s.to_s.length?

    In the more general case, why no just follow the pattern s.nil? ? 0 : =

    s.length?
     
    Paul McMahon, Mar 3, 2008
    #2
    1. Advertising

  3. On Mon, Mar 3, 2008 at 10:38 AM, Robert Klemme
    <> wrote:
    > Hi,
    >
    > we had frequent discussions about how to make code safely deal with
    > possible nil values. I remember having seen various solutions
    > proposed. However, I cannot remember having seen this, which just
    > occurred to me:
    >


    Raganwald proposed Object#andand and Object#me
    http://weblog.raganwald.com/2008/01/objectandand-objectme-in-ruby.html

    He also discusses Object#if_not_nil
    http://weblog.raganwald.com/2008/02/ifnotnil.html
    which was proposed by Fran=E7ois Beausoleil at
    http://blog.teksol.info/2007/11/23/a-little-smalltalk-in-ruby-if_nil-and-if=
    _not_nil
     
    Thomas Wieczorek, Mar 3, 2008
    #3
  4. Robert Klemme

    ThoML Guest

    Is there really an advantage over the more "traditional" solutions:

    x && x.foo
    x.foo if x
    x || 1
    x ? x.foo : 1
    x && x.foo || 1

    Apart from that I think one should rather deal with nils explicitly
    (and maybe a little bit earlier than in your examples).

    BTW I personally would rather prefer a statement that throws an error
    if an unchecked nil is assigned to a variable. (Which isn't too
    difficult to do.[1])

    Regards,
    Thomas.


    [1] http://groups.google.com/group/comp.lang.ruby/msg/9fe1b629764a196a
     
    ThoML, Mar 3, 2008
    #4
  5. On Mon, Mar 3, 2008 at 1:10 PM, ThoML <> wrote:
    > Is there really an advantage over the more "traditional" solutions:
    >


    I think it is more readable and you can chain additional method calls

    Vendors.find("location = 'Worms'").andand.products.find_all
     
    Thomas Wieczorek, Mar 3, 2008
    #5
  6. On 03.03.2008 11:20, Thomas Wieczorek wrote:
    > On Mon, Mar 3, 2008 at 10:38 AM, Robert Klemme
    > <> wrote:
    >> we had frequent discussions about how to make code safely deal with
    >> possible nil values. I remember having seen various solutions
    >> proposed. However, I cannot remember having seen this, which just
    >> occurred to me:

    >
    > Raganwald proposed Object#andand and Object#me
    > http://weblog.raganwald.com/2008/01/objectandand-objectme-in-ruby.html
    >
    > He also discusses Object#if_not_nil
    > http://weblog.raganwald.com/2008/02/ifnotnil.html
    > which was proposed by Fran├žois Beausoleil at
    > http://blog.teksol.info/2007/11/23/a-little-smalltalk-in-ruby-if_nil-and-if_not_nil


    Ah, good point! Thanks for the links. I guess that solution is just
    too obvious - I just could not remember having seen it. I have to take
    my Voltax... ;-)

    Kind regards

    robert
     
    Robert Klemme, Mar 3, 2008
    #6
    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. Brian Candler

    puts nil generates "nil\n"

    Brian Candler, Nov 6, 2004, in forum: Ruby
    Replies:
    1
    Views:
    120
  2. John Carter
    Replies:
    64
    Views:
    681
    Klaus Stein
    May 19, 2005
  3. ako...

    a == nil or a.nil?

    ako..., Nov 22, 2005, in forum: Ruby
    Replies:
    6
    Views:
    156
    Douglas Livingstone
    Nov 23, 2005
  4. Christoffer Sawicki
    Replies:
    5
    Views:
    275
    Christoffer Sawicki
    Sep 2, 2006
  5. Eero Saynatkari

    nil.to_s != "nil"

    Eero Saynatkari, Oct 27, 2006, in forum: Ruby
    Replies:
    11
    Views:
    245
    Gregory Brown
    Oct 28, 2006
Loading...

Share This Page