Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In Ruby repeated fields, each_index actually iterates over the index (#11767) #15190

Merged
1 commit merged into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions ruby/compatibility_tests/v3.0.0/tests/repeated_field_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,20 @@ def test_each
end


def test_each_index
m = TestMessage.new
5.times{|i| m.repeated_string << 'string' }

expected = 0
m.repeated_string.each_index do |idx|
assert_equal expected, idx
expected += 1
assert_equal 'string', m.repeated_string[idx]
end
assert_equal 5, expected
end


def test_empty?
m = TestMessage.new
assert_empty m.repeated_string
Expand Down
3 changes: 1 addition & 2 deletions ruby/lib/google/protobuf/repeated_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ def empty?
end

# array aliases into enumerable
alias_method :each_index, :each_with_index
alias_method :slice, :[]
alias_method :values_at, :select
alias_method :map, :collect
Expand Down Expand Up @@ -145,7 +144,7 @@ def define_array_wrapper_with_result_method(method_name)
end


%w(collect! compact! delete_if fill flatten! insert reverse!
%w(collect! compact! delete_if each_index fill flatten! insert reverse!
rotate! select! shuffle! sort! sort_by! uniq!).each do |method_name|
define_array_wrapper_with_result_method(method_name)
end
Expand Down
14 changes: 14 additions & 0 deletions ruby/tests/repeated_field_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,20 @@ def test_each
end


def test_each_index
m = TestMessage.new
5.times{|i| m.repeated_string << 'string' }

expected = 0
m.repeated_string.each_index do |idx|
assert_equal expected, idx
expected += 1
assert_equal 'string', m.repeated_string[idx]
end
assert_equal 5, expected
end


def test_empty?
m = TestMessage.new
assert_empty m.repeated_string
Expand Down
Loading