Changeset 9210
- Timestamp:
- 04/02/08 12:47:52 (8 months ago)
- Files:
-
- trunk/activesupport/CHANGELOG (modified) (1 diff)
- trunk/activesupport/lib/active_support/core_ext/hash/slice.rb (modified) (2 diffs)
- trunk/activesupport/test/core_ext/hash_ext_test.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activesupport/CHANGELOG
r9209 r9210 1 1 *SVN* 2 3 * Adding Hash#without Closes #7369 [eventualbuddha]4 2 5 3 * 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 12 12 # 13 13 # search(options.slice(:mass, :velocity, :time)) 14 #15 # Also allows leaving out certain keys. This is useful when duplicating16 # a hash but omitting a certain subset:17 #18 # Event.new(event.attributes.without(:id, :user_id))19 14 module Slice 20 15 # Returns a new hash with only the given keys. … … 28 23 replace(slice(*keys)) 29 24 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 end36 37 # Replaces the hash without the given keys.38 def without!(*keys)39 replace(without(*keys))40 end41 25 end 42 26 end trunk/activesupport/test/core_ext/hash_ext_test.rb
r9209 r9210 310 310 assert_equal expected, original.except!(:c) 311 311 assert_equal expected, original 312 end313 314 def test_without315 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, original321 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, original328 end329 330 def test_indifferent_without331 original = { :a => 'x', :b => 'y', :c => 10 }.with_indifferent_access332 expected = { :c => 10 }.with_indifferent_access333 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.inspect337 assert_not_equal expected, original338 339 # Should replace the hash without the given keys.340 copy = original.dup341 assert_equal expected, copy.without!(*keys)342 assert_equal expected, copy343 end344 312 end 345 313 end