public
Description: Koz's rails git-svn clone
Homepage: http://www.rubyonrails.org
Clone URL: git://github.com/NZKoz/koz-rails.git
Adding Hash#without Closes #7369 [eventualbuddha]


git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9209 
5ecf4fe2-1ee6-0310-87b1-e25e094e27de
pratik (author)
Wed Apr 02 04:45:03 -0700 2008
commit  85a5daef19ca727e4f2120a850f64613034f9ecc
tree    50e4b4299b71241e46a24ce36b0e8a66d67ba277
parent  81ff6ac8dbb6f753a78402dbc4d4f54ce4083b03
...
1
2
 
 
3
4
5
...
1
2
3
4
5
6
7
0
@@ -1,5 +1,7 @@
0
 *SVN*
0
 
0
+* Adding Hash#without Closes #7369 [eventualbuddha]
0
+
0
 * TimeWithZone#method_missing: send to utc to advance with dst correctness, otherwise send to time. Adding tests for time calculations methods [Geoff Buesing]
0
 
0
 * Add config.active_support.use_standard_json_time_format setting so that Times and Dates export to ISO 8601 dates. [rick]
...
11
12
13
 
 
 
 
 
14
15
16
...
22
23
24
 
 
 
 
 
 
 
 
 
 
 
25
26
27
...
11
12
13
14
15
16
17
18
19
20
21
...
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
0
@@ -11,6 +11,11 @@ module ActiveSupport #:nodoc:
0
       # end
0
       #
0
       # search(options.slice(:mass, :velocity, :time))
0
+ #
0
+ # Also allows leaving out certain keys. This is useful when duplicating
0
+ # a hash but omitting a certain subset:
0
+ #
0
+ # Event.new(event.attributes.without(:id, :user_id))
0
       module Slice
0
         # Returns a new hash with only the given keys.
0
         def slice(*keys)
0
@@ -22,6 +27,17 @@ module ActiveSupport #:nodoc:
0
         def slice!(*keys)
0
           replace(slice(*keys))
0
         end
0
+
0
+ # Returns a new hash without the given keys.
0
+ def without(*keys)
0
+ allowed = self.keys - (respond_to?(:convert_key) ? keys.map { |key| convert_key(key) } : keys)
0
+ slice(*allowed)
0
+ end
0
+
0
+ # Replaces the hash without the given keys.
0
+ def without!(*keys)
0
+ replace(without(*keys))
0
+ end
0
       end
0
     end
0
   end
...
310
311
312
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
313
314
315
...
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
0
@@ -310,6 +310,38 @@ class HashExtTest < Test::Unit::TestCase
0
     assert_equal expected, original.except!(:c)
0
     assert_equal expected, original
0
   end
0
+
0
+ def test_without
0
+ original = { :a => 'x', :b => 'y', :c => 10 }
0
+ expected = { :a => 'x' }
0
+
0
+ # Should return a hash without the given keys.
0
+ assert_equal expected, original.without(:b, :c)
0
+ assert_not_equal expected, original
0
+
0
+ # Should ignore non-existant keys.
0
+ assert_equal expected, original.without(:b, :c, :d)
0
+
0
+ # Should replace the hash with the given keys taken away.
0
+ assert_equal expected, original.without!(:b, :c)
0
+ assert_equal expected, original
0
+ end
0
+
0
+ def test_indifferent_without
0
+ original = { :a => 'x', :b => 'y', :c => 10 }.with_indifferent_access
0
+ expected = { :c => 10 }.with_indifferent_access
0
+
0
+ [['a', 'b'], [:a, :b]].each do |keys|
0
+ # Should return a new hash without the given keys.
0
+ assert_equal expected, original.without(*keys), keys.inspect
0
+ assert_not_equal expected, original
0
+
0
+ # Should replace the hash without the given keys.
0
+ copy = original.dup
0
+ assert_equal expected, copy.without!(*keys)
0
+ assert_equal expected, copy
0
+ end
0
+ end
0
 end
0
 
0
 class IWriteMyOwnXML

Comments

    No one has commented yet.