[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Dealing with nil

Robert Klemme

3/3/2008 9:39:00 AM

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

5 Answers

Paul McMahon

3/3/2008 9:58:00 AM

0

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

Thomas Wieczorek

3/3/2008 10:20:00 AM

0

On Mon, Mar 3, 2008 at 10:38 AM, Robert Klemme
<shortcutter@googlemail.com> 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...

He also discusses Object#if_not_nil
http://weblog.raganwald.com/2008/02/ifn...
which was proposed by Fran=E7ois Beausoleil at
http://blog.teksol.info/2007/11/23/a-little-smalltalk-in-ruby-if_n...
_not_nil

ThoML

3/3/2008 12:07:00 PM

0

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/9fe1b6...

Thomas Wieczorek

3/3/2008 12:44:00 PM

0

On Mon, Mar 3, 2008 at 1:10 PM, ThoML <micathom@gmail.com> 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

Robert Klemme

3/3/2008 7:20:00 PM

0

On 03.03.2008 11:20, Thomas Wieczorek wrote:
> On Mon, Mar 3, 2008 at 10:38 AM, Robert Klemme
> <shortcutter@googlemail.com> 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...
>
> He also discusses Object#if_not_nil
> http://weblog.raganwald.com/2008/02/ifn...
> which was proposed by François Beausoleil at
> http://blog.teksol.info/2007/11/23/a-little-smalltalk-in-ruby-if_nil-and-...

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