From: dblack@... Date: 2007-06-29T00:57:40+09:00 Subject: Re: Array Problem, sort Array ---2049402039-258736838-1183046258=:23351 Content-Type: MULTIPART/MIXED; BOUNDARY="-2049402039-258736838-1183046258=:23351" This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. ---2049402039-258736838-1183046258=:23351 Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE Hi -- On Thu, 28 Jun 2007, Pe=F1a, Botp wrote: > # > A little digression for everyone though... > # > irb(main):013:0> [nil].sort > # > =3D> [nil] > # > > # > is that intended? > # > # Well, you don't need the comparison operator where there's only one > # element, do you ? > > careful with the "one" there. The array returned may be complex. > eg, > irb(main):029:0> [[nil,[nil,"a"]]].sort > =3D> [[nil, [nil, "a"]]] > irb(main):030:0> [[nil,[nil,"a"]]].flatten.sort > NoMethodError: undefined method `<=3D>' for nil:NilClass > from (irb):30:in `sort' > > again, as i've said to Phrogz, the behavior hints that nil elements by th= emselves are sortable. Remember, programs contain blackboxes wc may be comp= lex and not obvious, eg > complex_proc_ret_array.sort_complex.foo_proc.bar_proc > > one could argue bluntly too, eg > [nil].sort > =3D>[nil] # yes, there is no need to sort > [nil,nil].sort > =3D>[nil,nil] # yes, there is no need to sort > > I really value consistency of object behavior when it comes to ruby, mayb= e because it's too dynamic and i want least number of surprises (especially= when you chain things). What about: class C; end [C.new].sort ? David --=20 * 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) ---2049402039-258736838-1183046258=:23351-- ---2049402039-258736838-1183046258=:23351--