Skip to content

Commit

Permalink
fix OrderedHash#each* methods to return Enumerators when called witho…
Browse files Browse the repository at this point in the history
…ut a block [#6366 state:resolved]

Signed-off-by: Santiago Pastorino <santiago@wyeworks.com>
  • Loading branch information
cldwalker authored and spastorino committed Feb 3, 2011
1 parent bca070e commit c0b4db0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
3 changes: 3 additions & 0 deletions activesupport/lib/active_support/ordered_hash.rb
Expand Up @@ -137,16 +137,19 @@ def to_a
end

def each_key
return to_enum(:each_key) unless block_given?
@keys.each { |key| yield key }
self
end

def each_value
return to_enum(:each_value) unless block_given?
@keys.each { |key| yield self[key]}
self
end

def each
return to_enum(:each) unless block_given?
@keys.each {|key| yield [key, self[key]]}
self
end
Expand Down
6 changes: 6 additions & 0 deletions activesupport/test/ordered_hash_test.rb
Expand Up @@ -80,18 +80,24 @@ def test_each_key
keys = []
assert_equal @ordered_hash, @ordered_hash.each_key { |k| keys << k }
assert_equal @keys, keys
expected_class = RUBY_VERSION < '1.9.1' ? Enumerable::Enumerator : Enumerator
assert_kind_of expected_class, @ordered_hash.each_key
end

def test_each_value
values = []
assert_equal @ordered_hash, @ordered_hash.each_value { |v| values << v }
assert_equal @values, values
expected_class = RUBY_VERSION < '1.9.1' ? Enumerable::Enumerator : Enumerator
assert_kind_of expected_class, @ordered_hash.each_value
end

def test_each
values = []
assert_equal @ordered_hash, @ordered_hash.each {|key, value| values << value}
assert_equal @values, values
expected_class = RUBY_VERSION < '1.9.1' ? Enumerable::Enumerator : Enumerator
assert_kind_of expected_class, @ordered_hash.each
end

def test_each_with_index
Expand Down

0 comments on commit c0b4db0

Please sign in to comment.