Permalink
Browse files

Support enumeration.

  • Loading branch information...
1 parent d2eea4a commit d85f4d630da26575d24c7c000eea0a456fac8668 @jsuchal committed Feb 6, 2012
Showing with 23 additions and 6 deletions.
  1. +8 −6 lib/hashugar.rb
  2. +15 −0 spec/hashugar_spec.rb
View
@@ -3,8 +3,11 @@
class Hashugar
def initialize(hash)
@table = {}
+ @table_with_original_keys = {}
hash.each_pair do |key, value|
- @table[convert_key(key)] = value.to_hashugar
+ hashugar = value.to_hashugar
+ @table_with_original_keys[key] = hashugar
+ @table[convert_key(key)] = hashugar
end
end
@@ -33,6 +36,10 @@ def respond_to?(key)
@table.has_key?(convert_key(key))
end
+ def each(&block)
+ @table_with_original_keys.each(&block)
+ end
+
private
def convert_key(key)
key.is_a?(Symbol) ? key.to_s : key
@@ -43,11 +50,6 @@ class Hash
def to_hashugar
Hashugar.new(self)
end
-
- private
- def convert_key(key)
- key.is_a?(Symbol) ? key.to_s : key
- end
end
class Array
View
@@ -74,4 +74,19 @@
end
end
+ context 'when enumerating' do
+ it 'should act like normal hash' do
+ hashugar = Hashugar.new({:a => 4, :c => 2})
+
+ keys = []
+ values = []
+ hashugar.each do |k, v|
+ keys << k
+ values << v
+ end
+
+ keys.should == [:a, :c]
+ values.should == [4, 2]
+ end
+ end
end

0 comments on commit d85f4d6

Please sign in to comment.