GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Rubygem
Description: Merb Core: All you need. None you don't.
Homepage: http://www.merbivore.com
Clone URL: git://github.com/wycats/merb-core.git
Avoid adding nil values in place of missing keys for Hash#only

Signed-off-by: Michael D. Ivey <ivey@gweezlebur.com>
binary42 (author)
Fri Jun 06 16:53:36 -0700 2008
ivey (committer)
Wed Jun 11 14:37:58 -0700 2008
commit  33a25d088ddf4f6931a509f8dee712d48bb045cd
tree    9d5189e6743c095c730f33ff4550b1d08a13c055
parent  ccb5a25af793b54b6173a3f443d96046684e48c1
...
127
128
129
130
 
131
132
133
...
127
128
129
 
130
131
132
133
0
@@ -127,7 +127,7 @@ class Hash
0
   # #=> { :one => 1 }
0
   def only(*allowed)
0
     hash = {}
0
- allowed.each {|k| hash[k] = self[k] }
0
+ allowed.each {|k| hash[k] = self[k] if self.has_key?(k) }
0
     hash
0
   end
0
 
...
17
18
19
20
 
21
22
23
 
 
24
25
26
...
17
18
19
 
20
21
22
23
24
25
26
27
28
0
@@ -17,10 +17,12 @@ end
0
 
0
 describe Hash, "only" do
0
   before do
0
- @hash = { :one => 'ONE', 'two' => 'TWO', 3 => 'THREE' }
0
+ @hash = { :one => 'ONE', 'two' => 'TWO', 3 => 'THREE', 4 => nil }
0
   end
0
 
0
   it "should return a hash with only the given key(s)" do
0
+ @hash.only(:not_in_there).should == {}
0
+ @hash.only(4).should == {4 => nil}
0
     @hash.only(:one).should == { :one => 'ONE' }
0
     @hash.only(:one, 3).should == { :one => 'ONE', 3 => 'THREE' }
0
   end

Comments

    No one has commented yet.