Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > unintended consequence: string.sort

Reply
Thread Tools

unintended consequence: string.sort

 
 
Martin DeMello
Guest
Posts: n/a
 
      07-05-2007
irb(main):001:0> "hello world".sort
=> ["hello world"]

(Because Enumerable#sort calls to_a, and String#to_a = [string])

However this is inconsistent with String#each, and almost certainly
never what the user wants. I'd suggest
String.split($/).sort.join($/).

martin

 
Reply With Quote
 
 
 
 
dblack@wobblini.net
Guest
Posts: n/a
 
      07-05-2007
Hi --

On Thu, 5 Jul 2007, Martin DeMello wrote:

> irb(main):001:0> "hello world".sort
> => ["hello world"]
>
> (Because Enumerable#sort calls to_a, and String#to_a = [string])
>
> However this is inconsistent with String#each, and almost certainly
> never what the user wants. I'd suggest
> String.split($/).sort.join($/).


I don't think it's inconsistent with String#each:

>> "hello world".each {|x| p x }

"hello world"

If you have multiple lines it will sort that way:

>> "hello\nworld".sort

=> ["hello\n", "world"]


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)

 
Reply With Quote
 
 
 
 
Martin DeMello
Guest
Posts: n/a
 
      07-05-2007
On 7/5/07, http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)> wrote:

> If you have multiple lines it will sort that way:
>
> >> "hello\nworld".sort

> => ["hello\n", "world"]


Huh - I thought I tried that and it didn't work. Would still be nicer
for it to return a string (since it can), but I agree it's consistent.

martin

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unintended Consequences steve NZ Computing 0 09-21-2006 12:07 PM
GCC-Bug ? (Unintended "overloading") Andreas Griesmayer C Programming 20 08-02-2006 01:20 AM
Unintended inheritance Kay Schluehr Python 2 02-24-2006 04:56 PM
Possible unintended reference comparison sotec ASP .Net 1 12-30-2005 09:02 PM
Spyware and unintended consequences Governor Swill Computer Support 4 01-11-2005 07:24 AM



Advertisments