Skip to content

Commit

Permalink
Fix RepeatedField#first in Ruby gem (#5293)
Browse files Browse the repository at this point in the history
Given an argument, the previous implementation was off by one
(`.first(2)` would return 3 elements) compared to the `Enumerable#first`
method.
  • Loading branch information
tobyhs authored and TeBoring committed Oct 29, 2018
1 parent 3025a06 commit 63d2f3b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ruby/lib/google/protobuf/repeated_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class RepeatedField


def first(n=nil)
n ? self[0..n] : self[0]
n ? self[0...n] : self[0]
end


Expand Down
8 changes: 8 additions & 0 deletions ruby/tests/repeated_field_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ def test_first
m = TestMessage.new
repeated_field_names(TestMessage).each do |field_name|
assert_nil m.send(field_name).first
assert_equal [], m.send(field_name).first(0)
assert_equal [], m.send(field_name).first(1)
end

fill_test_msg(m)
assert_equal -10, m.repeated_int32.first
assert_equal -1_000_000, m.repeated_int64.first
Expand All @@ -41,6 +44,11 @@ def test_first
assert_equal "bar".encode!('ASCII-8BIT'), m.repeated_bytes.first
assert_equal TestMessage2.new(:foo => 1), m.repeated_msg.first
assert_equal :A, m.repeated_enum.first

assert_equal [], m.repeated_int32.first(0)
assert_equal [-10], m.repeated_int32.first(1)
assert_equal [-10, -11], m.repeated_int32.first(2)
assert_equal [-10, -11], m.repeated_int32.first(3)
end


Expand Down

0 comments on commit 63d2f3b

Please sign in to comment.