Hi --

On Wed, 18 Jul 2007, Robert Dober wrote:

> 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).

My favorite slogan for Ruby is:

   The triumph of balance over symmetry.

It means, for example, that it might make sense for nil to have #to_s
-- and that it still might *not* make sense for nil to have #split.
One does not imply the other.  Every decision is made carefully, one
at a time, in the interest of the usefulness of the system overall.

That's why I don't care about symmetry or consistency (which I think
in this case mean much the same thing).  We're lucky enough to have
Matz hand-crafting the language with the greatest care; I think that's
a higher-percentage prospect than a principle of uniformity :-)


David

-- 
* Books:
   RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
   RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
     & consulting:  Ruby Power and Light, LLC (http://www.rubypal.com)