Joel VanderWerf
10/31/2007 11:35:00 PM
David A. Black wrote:
> Hi --
>
> On Thu, 1 Nov 2007, Daniel Waite wrote:
>
>> I was porting a small Python script over to Ruby and realized Ruby does
>> not sort strings as I expected it would.
>>
>> 'cba'.sort # ["cba"]
>>
>> So I wrote this...
>>
>> class String
>>
>> def sort
>> bytes = Array.new
>> self.each_byte { |byte| bytes << byte }
>> bytes.sort.collect { |byte| byte.chr }.join
>> end
>>
>> end
>>
>> 'cba'.sort # "abc"
>
> I wouldn't overwrite a core method like that; you could end up with
> some very unexpected results. It's better to give it a different name.
Also, note that String#sort depends on String#to_a being defined, which
is no longer true in 1.9. It's kind of an accident that "cba".sort works
at all.
I guess that means someone will be free to implement String#sort, as
long as they stick to 1.9. Maybe there will be a core implementation
that works as expected?
--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407