Dealing with nil

R

Robert Klemme

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
 
P

Paul McMahon

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?
 
T

Thomas Wieczorek

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
 
T

ThoML

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
 
T

Thomas Wieczorek

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
 
R

Robert Klemme


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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top