Skip to content

Commit 0c340b4

Browse files
koushiro615
authored andcommitted
ARROW-4593: [Ruby] Arrow::Array#[out_of_range] returns nil
Author: Kouhei Sutou <kou@clear-code.com> Closes #3666 from kou/ruby-array-ref-out-of-range and squashes the following commits: f64df5b <Kouhei Sutou> Array# returns nil
1 parent aa765aa commit 0c340b4

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

ruby/red-arrow/lib/arrow/array.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,18 @@ def new(*args)
3535
end
3636
end
3737

38+
# @param i [Integer]
39+
# The index of the value to be gotten.
40+
#
41+
# You can specify negative index like for `::Array#[]`.
42+
#
43+
# @return [Object, nil]
44+
# The `i`-th value.
45+
#
46+
# `nil` for NULL value or out of range `i`.
3847
def [](i)
3948
i += length if i < 0
49+
return nil if i < 0 or i >= length
4050
if null?(i)
4151
nil
4252
else

ruby/red-arrow/test/test-array.rb

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,30 @@ class ArrayTest < Test::Unit::TestCase
2424
end
2525
end
2626

27-
test("#each") do
28-
array = Arrow::BooleanArray.new([true, false, nil, true])
29-
assert_equal([true, false, nil, true],
30-
array.to_a)
31-
end
27+
sub_test_case("instance methods") do
28+
def setup
29+
@values = [true, false, nil, true]
30+
@array = Arrow::BooleanArray.new(@values)
31+
end
32+
33+
test("#each") do
34+
assert_equal(@values, @array.to_a)
35+
end
3236

33-
test("#[]") do
34-
array = Arrow::BooleanArray.new([true, false, nil, true])
35-
assert_equal([true, false, nil, true],
36-
[array[0], array[1], array[2], array[3]])
37+
sub_test_case("#[]") do
38+
test("valid range") do
39+
assert_equal(@values,
40+
@array.length.times.collect {|i| @array[i]})
41+
end
42+
43+
test("out of range") do
44+
assert_nil(@array[@array.length])
45+
end
46+
47+
test("negative index") do
48+
assert_equal(@values.last,
49+
@array[-1])
50+
end
51+
end
3752
end
3853
end

0 commit comments

Comments
 (0)