Skip to content
Closed
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
22 changes: 15 additions & 7 deletions ruby/red-arrow/lib/arrow/struct-array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions ruby/red-arrow/lib/arrow/struct.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 16 additions & 3 deletions ruby/red-arrow/test/test-struct-array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down