diff --git a/ruby/red-arrow/lib/arrow/struct-array.rb b/ruby/red-arrow/lib/arrow/struct-array.rb index e55a507868f..92b02bb37e1 100644 --- a/ruby/red-arrow/lib/arrow/struct-array.rb +++ b/ruby/red-arrow/lib/arrow/struct-array.rb @@ -19,17 +19,25 @@ module Arrow class StructArray - def [](i) - warn("Use #{self.class}\#find_field instead. " + - "This will returns Arrow::Struct instead of Arrow::Array " + - "since 0.13.0.") - get_field(i) - end - + # @param i [Integer] + # The index of the value to be gotten. You must specify the value index. + # + # You can use {Arrow::Array#[]} for convenient value access. + # + # @return [Arrow::Struct] The `i`-th value. def get_value(i) Struct.new(self, i) end + # @overload find_field(index) + # @param index [Integer] The index of the field to be found. + # @return [Arrow::Array, nil] + # The `index`-th field or `nil` for out of range. + # + # @overload find_field(name) + # @param index [String, Symbol] The name of the field to be found. + # @return [Arrow::Array, nil] + # The field that has `name` or `nil` for nonexistent name. def find_field(index_or_name) case index_or_name when String, Symbol diff --git a/ruby/red-arrow/lib/arrow/struct.rb b/ruby/red-arrow/lib/arrow/struct.rb index 4ae12b871e4..6028a7ba7f6 100644 --- a/ruby/red-arrow/lib/arrow/struct.rb +++ b/ruby/red-arrow/lib/arrow/struct.rb @@ -64,5 +64,16 @@ def method_missing(name, *args, &block) end super end + + def ==(other) + other.is_a?(self.class) and + @array == other.array and + @index == other.index + end + + protected + def array + @array + end end end diff --git a/ruby/red-arrow/test/test-struct-array.rb b/ruby/red-arrow/test/test-struct-array.rb index 5a00434713a..b82d048695f 100644 --- a/ruby/red-arrow/test/test-struct-array.rb +++ b/ruby/red-arrow/test/test-struct-array.rb @@ -49,9 +49,22 @@ def setup end test("#[]") do - notify("TODO: Returns Arrow::Struct instead.") - assert_equal([[true, false], [1, 2]], - [@array[0].to_a, @array[1].to_a]) + assert_equal([ + Arrow::Struct.new(@array, 0), + Arrow::Struct.new(@array, 1), + ], + @array.to_a) + end + + test("#get_value") do + assert_equal([ + Arrow::Struct.new(@array, 0), + Arrow::Struct.new(@array, 1), + ], + [ + @array.get_value(0), + @array.get_value(1), + ]) end sub_test_case("#find_field") do