Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 9210

Show
Ignore:
Timestamp:
04/02/08 12:47:52 (8 months ago)
Author:
pratik
Message:

Revert [9209] Use Hash#except

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activesupport/CHANGELOG

    r9209 r9210  
    11*SVN* 
    2  
    3 * Adding Hash#without Closes #7369 [eventualbuddha] 
    42 
    53* TimeWithZone#method_missing: send to utc to advance with dst correctness, otherwise send to time. Adding tests for time calculations methods [Geoff Buesing] 
  • trunk/activesupport/lib/active_support/core_ext/hash/slice.rb

    r9209 r9210  
    1212      # 
    1313      #   search(options.slice(:mass, :velocity, :time)) 
    14       # 
    15       # Also allows leaving out certain keys. This is useful when duplicating 
    16       # a hash but omitting a certain subset: 
    17       # 
    18       #   Event.new(event.attributes.without(:id, :user_id)) 
    1914      module Slice 
    2015        # Returns a new hash with only the given keys. 
     
    2823          replace(slice(*keys)) 
    2924        end 
    30  
    31         # Returns a new hash without the given keys. 
    32         def without(*keys) 
    33           allowed = self.keys - (respond_to?(:convert_key) ? keys.map { |key| convert_key(key) } : keys) 
    34           slice(*allowed) 
    35         end 
    36  
    37         # Replaces the hash without the given keys. 
    38         def without!(*keys) 
    39           replace(without(*keys)) 
    40         end 
    4125      end 
    4226    end 
  • trunk/activesupport/test/core_ext/hash_ext_test.rb

    r9209 r9210  
    310310    assert_equal expected, original.except!(:c) 
    311311    assert_equal expected, original 
    312   end 
    313  
    314   def test_without 
    315     original = { :a => 'x', :b => 'y', :c => 10 } 
    316     expected = { :a => 'x' } 
    317  
    318     # Should return a hash without the given keys. 
    319     assert_equal expected, original.without(:b, :c) 
    320     assert_not_equal expected, original 
    321  
    322     # Should ignore non-existant keys. 
    323     assert_equal expected, original.without(:b, :c, :d) 
    324  
    325     # Should replace the hash with the given keys taken away. 
    326     assert_equal expected, original.without!(:b, :c) 
    327     assert_equal expected, original 
    328   end 
    329  
    330   def test_indifferent_without 
    331     original = { :a => 'x', :b => 'y', :c => 10 }.with_indifferent_access 
    332     expected = { :c => 10 }.with_indifferent_access 
    333  
    334     [['a', 'b'], [:a, :b]].each do |keys| 
    335       # Should return a new hash without the given keys. 
    336       assert_equal expected, original.without(*keys), keys.inspect 
    337       assert_not_equal expected, original 
    338  
    339       # Should replace the hash without the given keys. 
    340       copy = original.dup 
    341       assert_equal expected, copy.without!(*keys) 
    342       assert_equal expected, copy 
    343     end 
    344312  end 
    345313end