--nextPart1561589.L36uTRmXCA Content-Type: text/plain; charset so-8859-1" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Quoth Joel VanderWerf: > 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 Well, anyone who's sorting a string really just wants to sort the component letters, so my_str.split('').sort.join('') is the way to go. If they want to add a method to String to simplify this, they can do that. -- Konrad Meyer <konrad / tylerc.org> http://konrad.sobertillnoon.com/ --nextPart1561589.L36uTRmXCA Content-Type: application/pgp-signature; name=signature.asc Content-Description: This is a digitally signed message part. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (GNU/Linux) iD8DBQBHKRPSCHB0oCiR2cwRAkh/AJ0fwxt4DFEVSEJ9u8voFixSdnqwQwCgyOfH ZNmeRH4lCQYxnfJim93+A5A ¨Â-----END PGP SIGNATURE----- --nextPart1561589.L36uTRmXCA--