Created
October 31, 2010 23:39
-
-
Save 13k/657319 to your computer and use it in GitHub Desktop.
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 'rubygems' | |
| require 'thor' | |
| require 'active_support/core_ext/hash' | |
| h1 = {:a => 1} | |
| # => {:a=>1} | |
| h2 = Thor::CoreExt::HashWithIndifferentAccess.new(:a => 2) | |
| # => {"a"=>2} | |
| h3 = ActiveSupport::HashWithIndifferentAccess.new(:a => 3) | |
| # => {"a"=>3} | |
| h1.merge(h2) | |
| # => {:a=>1, "a"=>2} | |
| h1.merge(h3) | |
| # => {:a=>1, "a"=>3} | |
| h2.reverse_merge :a => 13 | |
| # => {:a=>13, "a"=>2} | |
| h3.reverse_merge :a => 13 | |
| # => {"a"=>3} | |
| def method1(opts={}) | |
| opts = { | |
| :default => :options, | |
| :are => :not_merged | |
| }.merge(opts) | |
| end | |
| opts = {:default => :choices, :are => :merged} | |
| thor_opts = Thor::CoreExt::HashWithIndifferentAccess.new(:default => :choices, :are => :string_indexed_and_merged) | |
| as_opts = ActiveSupport::HashWithIndifferentAccess.new(:default => :choices, :are => :string_indexed_and_merged) | |
| method1(opts) | |
| # => {:default=>:choices, :are=>:merged} | |
| method1(thor_opts) | |
| # => {:default=>:options, :are=>:not_merged, "default"=>:choices, "are"=>:string_indexed_and_merged} | |
| method1(as_opts) | |
| # => {:default=>:options, :are=>:not_merged, "default"=>:choices, "are"=>:string_indexed_and_merged} | |
| ### Working with the usual default_options_hash.merge(user_options) won't work with neither HashWithIndifferentAccess versions. #symbolize_keys works on AS version, though: | |
| # Thor version won't work | |
| thor_opts.symbolize_keys | |
| # => {"default"=>:choices, "are"=>:string_indexed_and_merged} | |
| method1(thor_opts.symbolize_keys) | |
| # => {:default=>:options, :are=>:not_merged, "default"=>:choices, "are"=>:string_indexed_and_merged} | |
| # AS version works | |
| as_opts.symbolize_keys | |
| # => {:default=>:choices, :are=>:merged} | |
| method1(as_opts.symbolize_keys) | |
| # => {:default=>:choices, :are=>:merged} | |
| ### Now a second method using ActiveSupport's #reverse_merge | |
| def method2(opts={}) | |
| opts = opts.reverse_merge :default => :options, :are => :not_merged | |
| end | |
| opts = {:default => :choices, :are => :reverse_merged} | |
| thor_opts = Thor::CoreExt::HashWithIndifferentAccess.new(:default => :choices, :are => :string_indexed_and_reverse_merged) | |
| as_opts = ActiveSupport::HashWithIndifferentAccess.new(:default => :choices, :are => :reverse_merged) | |
| method2(opts) | |
| # => {"default"=>:choices, "are"=>:reverse_merged} | |
| method2(thor_opts) | |
| # => {:default=>:options, :are=>:not_merged, "default"=>:choices, "are"=>:string_indexed_and_reverse_merged} | |
| method2(as_opts) | |
| # => {"default"=>:choices, "are"=>:reverse_merged} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment