unintended consequence: string.sort

M

Martin DeMello

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
 
D

dblack

Hi --

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"

If you have multiple lines it will sort that way:
=> ["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)
 
M

Martin DeMello

If you have multiple lines it will sort that way:
=> ["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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

is it bug? for 14
Range oddity 3
[SUMMARY] hexdump (#171) 0
Enumerable#serially - those nifty functions w/o memory footprint 12
Crossword 14
question regarding sub!() 3
regexp widehex glitch 0
qtruby problems 4

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top