Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mrnugget
Last active February 1, 2016 15:52
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 mrnugget/3272fbede054ae2cd38c to your computer and use it in GitHub Desktop.
Save mrnugget/3272fbede054ae2cd38c to your computer and use it in GitHub Desktop.
Ruby 2.3 - sort_by "Bug" on OS X and Linux. The behaviour of `sort_by` has changed with Ruby 2.3 on OS X if the block passed to `sort_by` returns the same value for multiple values.
# Ruby 2.3.0 - OS X
`uname -a`
# => "Darwin keef.local 15.3.0 Darwin Kernel Version 15.3.0: Thu Dec 10 18:40:58 PST 2015; root:xnu-3248.30.4~1/RELEASE_X86_64 x86_64\n"
puts RUBY_VERSION
# 2.3.0
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10].sort_by {|element| nil }
# => [10, 2, 3, 4, 5, 6, 7, 8, 9, 1] --- First and last elements swapped
# Ruby 2.3.0 - Linux
irb(main):001:0> `uname -a`
# "Linux awesome-linux-host 3.13.0-74-generic #118-Ubuntu SMP Thu Dec 17 22:52:10 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux\n"
irb(main):002:0> puts RUBY_VERSION
# 2.3.0
=> nil
irb(main):003:0> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].sort_by {|element| nil }
# => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] --- Still the same order
# Ruby 2.2.3 - OS X
>> `uname -a`
# "Darwin keef.local 15.3.0 Darwin Kernel Version 15.3.0: Thu Dec 10 18:40:58 PST 2015; root:xnu-3248.30.4~1/RELEASE_X86_64 x86_64\n"
>> puts RUBY_VERSION
# 2.2.3
>> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].sort_by {|element| nil }
# [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] --- Still the same order
# Ruby 2.2.3 - Linux
>> `uname -a`
# "Linux ubuntu 3.19.0-47-generic #53~14.04.1-Ubuntu SMP Mon Jan 18 16:09:14 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux\n"
>> puts RUBY_VERSION
# 2.2.3
>> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].sort_by {|element| nil }
# [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] --- Still the same order
### More Information:
# https://medium.com/@barelyknown/ruby-2-3-0-bug-in-array-sort-by-38a97b28f02#.6zu1xejy3
# https://bugs.ruby-lang.org/issues/11907
@vincemtnz
Copy link

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