Last active
January 29, 2016 18:52
-
-
Save robacarp/68b7cdea1fcc3de31a2b to your computer and use it in GitHub Desktop.
Ruby splat in 2.2.2 is broken
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # require 'byebug' | |
| puts "ruby -v is #{RUBY_VERSION} but should be '2.2.2'" unless RUBY_VERSION == '2.2.2' | |
| class Hash | |
| def symbolize_keys | |
| dup = self.class.new | |
| each_key do |key| | |
| dup[ key.to_sym ] = self[key] | |
| end | |
| dup | |
| # Other implementations tried: | |
| # to_a.map{ |pair| pair[0] = pair[0].to_sym; pair }.to_h | |
| end | |
| end | |
| class MagicDance | |
| def initialize | |
| @rolled_back = false | |
| end | |
| def dance(one:, two:, **splat) | |
| puts "#{two} #{one}" | |
| puts splat | |
| end | |
| def one | |
| # ============================ | |
| # Buggy, but only when it's by itself in the method: | |
| splat = { "three" => :anything }.symbolize_keys | |
| dance two: :not, one: :broken, **splat | |
| # ============================ | |
| # Uncomment to see above statement succeed | |
| # dance two: :not, one: :broken, **{ three: :anything } | |
| # ============================ | |
| # Uncomment to see above statement succeed | |
| # dance two: :not, one: :broken, three: :anything | |
| end | |
| # Even in different methods! | |
| # Uncomment to see above statement succeed | |
| # def two | |
| # dance two: :not, one: :broken, **{ three: :anything } | |
| # end | |
| end | |
| MagicDance.new.one |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment