Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Dealing with nil

Reply
Thread Tools

Dealing with nil

 
 
Robert Klemme
Guest
Posts: n/a
 
      03-03-2008
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

 
Reply With Quote
 
 
 
 
Paul McMahon
Guest
Posts: n/a
 
      03-03-2008
> 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?

 
Reply With Quote
 
 
 
 
Thomas Wieczorek
Guest
Posts: n/a
 
      03-03-2008
On Mon, Mar 3, 2008 at 10:38 AM, Robert Klemme
<(E-Mail Removed)> 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/...e-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...if_nil-and-if=
_not_nil

 
Reply With Quote
 
ThoML
Guest
Posts: n/a
 
      03-03-2008
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....e1b629764a196a
 
Reply With Quote
 
Thomas Wieczorek
Guest
Posts: n/a
 
      03-03-2008
On Mon, Mar 3, 2008 at 1:10 PM, ThoML <(E-Mail Removed)> 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

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      03-03-2008
On 03.03.2008 11:20, Thomas Wieczorek wrote:
> On Mon, Mar 3, 2008 at 10:38 AM, Robert Klemme
> <(E-Mail Removed)> 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/...e-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...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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
nil.to_s != "nil" Eero Saynatkari Ruby 11 10-28-2006 10:57 PM
Integer(nil) versus Float(nil) versus String(nil) Christoffer Sawicki Ruby 5 09-02-2006 06:28 PM
a == nil or a.nil? ako... Ruby 6 11-23-2005 05:03 AM
RCR 303: nil should accept missing methods and return nil John Carter Ruby 64 05-19-2005 12:12 PM
puts nil generates "nil\n" Brian Candler Ruby 1 11-06-2004 01:59 PM



Advertisments