diff options
author | Frederick Cheung <frederick.cheung@gmail.com> | 2008-12-11 15:19:03 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2008-12-15 15:34:29 +0100 |
commit | 4dcd8f01afe5800baa67bbdf72832afb0d627755 (patch) | |
tree | 7f0a67dea09240b8fd1bb4ba6f338971345fd850 /activesupport/lib | |
parent | 49306ccacf01e36d444771d42321965616e226f0 (diff) | |
download | rails-4dcd8f01afe5800baa67bbdf72832afb0d627755.tar.gz rails-4dcd8f01afe5800baa67bbdf72832afb0d627755.tar.bz2 rails-4dcd8f01afe5800baa67bbdf72832afb0d627755.zip |
Make delete_if/reject faster and fix other mutators
[#1559 state:committed]
Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support/ordered_hash.rb | 37 |
1 files changed, 24 insertions, 13 deletions
diff --git a/activesupport/lib/active_support/ordered_hash.rb b/activesupport/lib/active_support/ordered_hash.rb index fa68db5604..3def0be639 100644 --- a/activesupport/lib/active_support/ordered_hash.rb +++ b/activesupport/lib/active_support/ordered_hash.rb @@ -18,19 +18,13 @@ module ActiveSupport end def delete(key) - array_index = has_key?(key) && index(key) - if array_index - @keys.delete_at(array_index) + if has_key? key + index = @keys.index(key) + @keys.delete_at index end super end - def delete_if - super - sync_keys! - self - end - def reject! super sync_keys! @@ -41,9 +35,6 @@ module ActiveSupport dup.reject!(&block) end - alias_method :super_keys, :keys - private :super_keys - def keys @keys end @@ -68,10 +59,30 @@ module ActiveSupport keys.each {|key| yield [key, self[key]]} end + alias_method :each_pair, :each + + def clear + super + @keys.clear + self + end + + def shift + k = @keys.first + v = delete(k) + [k, v] + end + + def merge(other_hash) + result = dup + other_hash.each {|k,v| result[k]=v} + result + end + private def sync_keys! - (@keys - super_keys).each { |k| @keys.delete(k) } + @keys.delete_if {|k| !has_key?(k)} end end end |