Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Hash#reverse_merge, Hash#reverse_merge!, and Hash#reverse_updat…
…e to ease the use of default options. Added :connector and :skip_last_comma options to Array#to_sentence git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2192 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
- Loading branch information
Showing
6 changed files
with
56 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,7 +1,9 @@ | |||
require File.dirname(__FILE__) + '/hash/keys' | require File.dirname(__FILE__) + '/hash/keys' | ||
require File.dirname(__FILE__) + '/hash/indifferent_access' | require File.dirname(__FILE__) + '/hash/indifferent_access' | ||
require File.dirname(__FILE__) + '/hash/reverse_merge' | |||
|
|
||
class Hash #:nodoc: | class Hash #:nodoc: | ||
include ActiveSupport::CoreExtensions::Hash::Keys | include ActiveSupport::CoreExtensions::Hash::Keys | ||
include ActiveSupport::CoreExtensions::Hash::IndifferentAccess | include ActiveSupport::CoreExtensions::Hash::IndifferentAccess | ||
include ActiveSupport::CoreExtensions::Hash::ReverseMerge | |||
end | end |
25 changes: 25 additions & 0 deletions
25
activesupport/lib/active_support/core_ext/hash/reverse_merge.rb
This file contains 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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,25 @@ | |||
module ActiveSupport #:nodoc: | |||
module CoreExtensions #:nodoc: | |||
module Hash #:nodoc: | |||
# 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. | |||
module ReverseMerge | |||
def reverse_merge(other_hash) | |||
other_hash.merge(self) | |||
end | |||
|
|||
def reverse_merge!(other_hash) | |||
replace(reverse_merge(other_hash)) | |||
end | |||
|
|||
alias_method :reverse_update, :reverse_merge | |||
end | |||
end | |||
end | |||
end |
This file contains 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
This file contains 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