diff options
author | Brian Abreu <brian@nutsonline.com> | 2009-06-24 10:51:20 -0700 |
---|---|---|
committer | Yehuda Katz + Carl Lerche <ykatz+clerche@engineyard.com> | 2009-07-02 12:01:05 -0700 |
commit | d03689971758b905fa0087bc93cf474a9d0585f5 (patch) | |
tree | 44fe5107872eb2527e9bc40cdf7e52106cbacf1d /activesupport/lib | |
parent | e61afed6f8d2ef6a580ba00a5beaaaf0bf2ddb9a (diff) | |
download | rails-d03689971758b905fa0087bc93cf474a9d0585f5.tar.gz rails-d03689971758b905fa0087bc93cf474a9d0585f5.tar.bz2 rails-d03689971758b905fa0087bc93cf474a9d0585f5.zip |
Fixed ActiveSupport::OrderedHash::[] work identically to ::Hash::[] in ruby 1.8.7 [#2832 state:resolved]
Signed-off-by: Yehuda Katz + Carl Lerche <ykatz+clerche@engineyard.com>
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support/ordered_hash.rb | 22 |
1 files changed, 18 insertions, 4 deletions
diff --git a/activesupport/lib/active_support/ordered_hash.rb b/activesupport/lib/active_support/ordered_hash.rb index 8d1c0f5160..4324e40cbb 100644 --- a/activesupport/lib/active_support/ordered_hash.rb +++ b/activesupport/lib/active_support/ordered_hash.rb @@ -12,11 +12,25 @@ module ActiveSupport def self.[](*args) ordered_hash = new - args.each_with_index { |val,ind| - # Only every second value is a key. - next if ind % 2 != 0 + + if (args.length == 1 && args.first.is_a?(Array)) + args.first.each do |key_value_pair| + next unless (key_value_pair.is_a?(Array)) + ordered_hash[key_value_pair[0]] = key_value_pair[1] + end + + return ordered_hash + end + + unless (args.size % 2 == 0) + raise ArgumentError.new("odd number of arguments for Hash") + end + + args.each_with_index do |val, ind| + next if (ind % 2 != 0) ordered_hash[val] = args[ind + 1] - } + end + ordered_hash end |