Sha256: 6d9098b6da7926d5ff2eb9f93ed6110fc098a481cb07e70a2fb0e9ab8a4c2c72
Contents?: true
Size: 1.15 KB
Versions: 10
Compression:
Stored size: 1.15 KB
Contents
# TITLE: # # Reverse Merge # # SUMMARY: # # Like merge, but give the parameter priority over the # reciever. # # CREDITS: # # - Gavin Sinclair # - Thomas Sawyer class Hash # Allows for reverse merging where its the keys in the # calling hash that wins over those in the <tt>other_hash</tt>. # This is particularly useful for initializing an incoming # option hash with default values: # # def setup(options = {}) # options.reverse_merge! :size => 25, :velocity => 10 # end # # The default :size and :velocity is only set if the +options+ # passed in doesn't already have those keys set. def reverse_merge(other) other.merge(self) end def reverse_merge!(other) replace(reverse_merge(other)) end alias_method :reverse_update, :reverse_merge! end # _____ _ # |_ _|__ ___| |_ # | |/ _ \/ __| __| # | | __/\__ \ |_ # |_|\___||___/\__| # =begin test require 'test/unit' class TestHashReverseMerge < Test::Unit::TestCase def test_reverse_merge h1 = { :a=>1, :b=>2, :c=>2 } h2 = { :c=>3 } h3 = h1.reverse_merge(h2) ) assert_equal( 2, h3[:c] ) end end =end
Version data entries
10 entries across 10 versions & 1 rubygems