Skip to content

Commit 874916c

Browse files
committed
In Ruby repeated fields, each_index actually iterates over the index, not the values
1 parent a138e54 commit 874916c

3 files changed

Lines changed: 29 additions & 2 deletions

File tree

ruby/compatibility_tests/v3.0.0/tests/repeated_field_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,20 @@ def test_each
119119
assert_equal ['string'] * 5, result
120120
end
121121

122+
123+
def test_each_index
124+
m = TestMessage.new
125+
5.times{|i| m.repeated_string << 'string' }
126+
127+
expected = 0
128+
m.repeated_string.each_index do |idx|
129+
assert_equal expected, idx
130+
expected += 1
131+
assert_equal 'string', m.repeated_string[idx]
132+
end
133+
assert_equal 5, expected
134+
end
135+
122136

123137
def test_empty?
124138
m = TestMessage.new

ruby/lib/google/protobuf/repeated_field.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ def empty?
117117
end
118118

119119
# array aliases into enumerable
120-
alias_method :each_index, :each_with_index
121120
alias_method :slice, :[]
122121
alias_method :values_at, :select
123122
alias_method :map, :collect
@@ -168,7 +167,7 @@ def define_array_wrapper_with_result_method(method_name)
168167
end
169168

170169

171-
%w(collect! compact! delete_if fill flatten! insert reverse!
170+
%w(collect! compact! delete_if each_index fill flatten! insert reverse!
172171
rotate! select! shuffle! sort! sort_by! uniq!).each do |method_name|
173172
define_array_wrapper_with_result_method(method_name)
174173
end

ruby/tests/repeated_field_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,20 @@ def test_each
141141
end
142142

143143

144+
def test_each_index
145+
m = TestMessage.new
146+
5.times{|i| m.repeated_string << 'string' }
147+
148+
expected = 0
149+
m.repeated_string.each_index do |idx|
150+
assert_equal expected, idx
151+
expected += 1
152+
assert_equal 'string', m.repeated_string[idx]
153+
end
154+
assert_equal 5, expected
155+
end
156+
157+
144158
def test_empty?
145159
m = TestMessage.new
146160
assert_equal true, m.repeated_string.empty?

0 commit comments

Comments
 (0)