This is an automated email from the ASF dual-hosted git repository.

shiro pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/master by this push:
     new 0c340b4  ARROW-4593: [Ruby] Arrow::Array#[out_of_range] returns nil
0c340b4 is described below

commit 0c340b4a4a1bcd71c3c1a97415d6c06ceaf43c2d
Author: Kouhei Sutou <k...@clear-code.com>
AuthorDate: Sun Feb 17 10:03:52 2019 +0900

    ARROW-4593: [Ruby] Arrow::Array#[out_of_range] returns nil
    
    Author: Kouhei Sutou <k...@clear-code.com>
    
    Closes #3666 from kou/ruby-array-ref-out-of-range and squashes the 
following commits:
    
    f64df5b5 <Kouhei Sutou>  Array# returns nil
---
 ruby/red-arrow/lib/arrow/array.rb | 10 ++++++++++
 ruby/red-arrow/test/test-array.rb | 33 ++++++++++++++++++++++++---------
 2 files changed, 34 insertions(+), 9 deletions(-)

diff --git a/ruby/red-arrow/lib/arrow/array.rb 
b/ruby/red-arrow/lib/arrow/array.rb
index 359e70e..f60025a 100644
--- a/ruby/red-arrow/lib/arrow/array.rb
+++ b/ruby/red-arrow/lib/arrow/array.rb
@@ -35,8 +35,18 @@ module Arrow
       end
     end
 
+    # @param i [Integer]
+    #   The index of the value to be gotten.
+    #
+    #   You can specify negative index like for `::Array#[]`.
+    #
+    # @return [Object, nil]
+    #   The `i`-th value.
+    #
+    #   `nil` for NULL value or out of range `i`.
     def [](i)
       i += length if i < 0
+      return nil if i < 0 or i >= length
       if null?(i)
         nil
       else
diff --git a/ruby/red-arrow/test/test-array.rb 
b/ruby/red-arrow/test/test-array.rb
index 31e6eaf..3dd7635 100644
--- a/ruby/red-arrow/test/test-array.rb
+++ b/ruby/red-arrow/test/test-array.rb
@@ -24,15 +24,30 @@ class ArrayTest < Test::Unit::TestCase
     end
   end
 
-  test("#each") do
-    array = Arrow::BooleanArray.new([true, false, nil, true])
-    assert_equal([true, false, nil, true],
-                 array.to_a)
-  end
+  sub_test_case("instance methods") do
+    def setup
+      @values = [true, false, nil, true]
+      @array = Arrow::BooleanArray.new(@values)
+    end
+
+    test("#each") do
+      assert_equal(@values, @array.to_a)
+    end
 
-  test("#[]") do
-    array = Arrow::BooleanArray.new([true, false, nil, true])
-    assert_equal([true, false, nil, true],
-                 [array[0], array[1], array[2], array[3]])
+    sub_test_case("#[]") do
+      test("valid range") do
+        assert_equal(@values,
+                     @array.length.times.collect {|i| @array[i]})
+      end
+
+      test("out of range") do
+        assert_nil(@array[@array.length])
+      end
+
+      test("negative index") do
+        assert_equal(@values.last,
+                     @array[-1])
+      end
+    end
   end
 end

Reply via email to