On 7/18/07, Robert Klemme <shortcutter / googlemail.com> wrote:
> 2007/7/18, Jeff Pritchard <jp / jeffpritchard.com>:
> > Daniel Lucraft wrote:
> > >> On Jul 17, 6:52 pm, Jeff Pritchard <j... / jeffpritchard.com> wrote:
> > >>> This could be written:
> > >>> blah = (foo.bar||"").split
> > >
> > > Sometimes I think the brackets spoil how the expression reads. You could
> > > define a method that looks like this:
> > >
> > > blah = foo.bar.or("").split
> > >
> > > Trainwreck though...
> > >
> > > Dan
> >
> > Thanks everyone.  This has at least convinced me that I wasn't missing
> > some well known way to do this.
>
> I believe another (obvious?) solution has not been mentioned so far:
I believe Aur has ...
>
> # note, I used the empty array as replacement
> # because split would return an array
> blah = foo.bar.split rescue []
... and I think it is nice.

I lost the fight with myself to propose this :)

class Nil
  def split; [] end
  def join(x); "" end
  def each &blk; end
  etc.etc
end

now there might be many reasons against this, and I believe that they
outweight the benefits, but look at this

irb(main):005:0> nil.to_i
=> 0
irb(main):006:0> ## and worse
irb(main):007:0* Integer(nil)
=> 0

so I am still suffering from Ruby's inconsistency (do not laugh Lionel).

<snip>
robert


-- 
I always knew that one day Smalltalk would replace Java.
I just didn't know it would be called Ruby
-- Kent Beck