# Dealing with nil

**URL:** <https://www.ruby-forum.com/t/dealing-with-nil/130783>\
**Category:** Ruby\
**Created:** [March 3, 2008, 10:39am UTC](https://www.ruby-forum.com/t/dealing-with-nil/130783 "2008-03-03T10:39:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![robkuz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/34f0e0/32.png) [@robkuz](https://www.ruby-forum.com/u/robkuz)\
**Post date:** [March 3, 2008, 10:39am UTC](https://www.ruby-forum.com/t/dealing-with-nil/130783/1 "2008-03-03T10:39:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![robkuz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/34f0e0/32.png) [@robkuz](https://www.ruby-forum.com/u/robkuz)\
**Post date:** [March 3, 2008, 10:59am UTC](https://www.ruby-forum.com/t/dealing-with-nil/130783/2 "2008-03-03T10:59:01Z")

</div>

> With this, you can do
> 
> irb(main):038:0\> s=nil  
> =\> nil  
> irb(main):039:0\> s.nil\_safe(0) {length}  
> =\> 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?

---

<div class="post-metadata">

**Author:** ![robkuz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/34f0e0/32.png) [@robkuz](https://www.ruby-forum.com/u/robkuz)\
**Post date:** [March 3, 2008, 11:21am UTC](https://www.ruby-forum.com/t/dealing-with-nil/130783/3 "2008-03-03T11:21:00Z")

</div>

On Mon, Mar 3, 2008 at 10:38 AM, Robert K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](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](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](http://blog.teksol.info/2007/11/23/a-little-smalltalk-in-ruby-if_nil-and-if_not_nil)

---

<div class="post-metadata">

**Author:** ![robkuz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/34f0e0/32.png) [@robkuz](https://www.ruby-forum.com/u/robkuz)\
**Post date:** [March 3, 2008, 1:44pm UTC](https://www.ruby-forum.com/t/dealing-with-nil/130783/4 "2008-03-03T13:44:35Z")

</div>

On Mon, Mar 3, 2008 at 1:10 PM, ThoML [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![robkuz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/34f0e0/32.png) [@robkuz](https://www.ruby-forum.com/u/robkuz)\
**Post date:** [March 3, 2008, 8:28pm UTC](https://www.ruby-forum.com/t/dealing-with-nil/130783/5 "2008-03-03T20:28:12Z")

</div>

On 03.03.2008 11:20, Thomas W. wrote:

> He also discusses Object#if\_not\_nil  
> [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](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

---

<div class="post-metadata">

**Author:** ![robkuz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/34f0e0/32.png) [@robkuz](https://www.ruby-forum.com/u/robkuz)\
**Post date:** [March 3, 2008, 1:11pm UTC](https://www.ruby-forum.com/t/dealing-with-nil/130783/6 "2008-03-03T13:11:01Z")

</div>

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](http://groups.google.com/group/comp.lang.ruby/msg/9fe1b629764a196a)
