Skip to content

Instantly share code, notes, and snippets.

@lucascaton
Last active April 12, 2016 12:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucascaton/af44fc82e0b11a842874 to your computer and use it in GitHub Desktop.
Save lucascaton/af44fc82e0b11a842874 to your computer and use it in GitHub Desktop.
%w(1 2).sort_by { |i| nil }
# => ["1", "2"]
# ...
%w(1 2 3 4 5 6).sort_by { |i| nil }
# => ["1", "2", "3", "4", "5", "6"]
### So far, so good...
### But look what happens when you have 7 or more elements:
%w(1 2 3 4 5 6 7).sort_by { |i| nil }
# => ["4", "2", "3", "1", "5", "6", "7"]
%w(1 2 3 4 5 6 7 8).sort_by { |i| nil }
# => ["8", "2", "3", "4", "5", "6", "7", "1"]
%w(1 2 3 4 5 6 7 8 9).sort_by { |i| nil }
# => ["9", "2", "3", "4", "5", "6", "7", "8", "1"]
%w(1 2 3 4 5 6 7 8 9 10).sort_by { |i| nil }
# => ["10", "2", "3", "4", "5", "6", "7", "8", "9", "1"]
# 🤔
@cassiomarques
Copy link

Weird!

irb(main):001:0> (1..10).map(&:to_s).sort_by { |_| nil }
=> ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
irb(main):002:0> (1..5).map(&:to_s).sort_by { |_| nil }
=> ["1", "2", "3", "4", "5"]
irb(main):003:0> (1..20).map(&:to_s).sort_by { |_| nil }
=> ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"]
irb(main):004:0> (1..17).map(&:to_s).sort_by { |_| nil }
=> ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17"]
irb(main):005:0> RUBY_VERSION
=> "2.2.2"

What's the difference?

@rmoriz
Copy link

rmoriz commented Apr 12, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment