Skip to content

Commit aa765aa

Browse files
kouwesm
authored andcommitted
ARROW-4594: [Ruby] Arrow::StructArray#[] returns Arrow::Struct instead of Arrow::Array
This is a compatibility breaking change but we warn about this change in 0.12.0. Author: Kouhei Sutou <kou@clear-code.com> Closes #3667 from kou/ruby-struct-array-ref and squashes the following commits: 5b30449 <Kouhei Sutou> Arrow::StructArray# returns Arrow::Struct instead of Arrow::Array
1 parent b74cc65 commit aa765aa

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
lines changed

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,25 @@
1919

2020
module Arrow
2121
class StructArray
22-
def [](i)
23-
warn("Use #{self.class}\#find_field instead. " +
24-
"This will returns Arrow::Struct instead of Arrow::Array " +
25-
"since 0.13.0.")
26-
get_field(i)
27-
end
28-
22+
# @param i [Integer]
23+
# The index of the value to be gotten. You must specify the value index.
24+
#
25+
# You can use {Arrow::Array#[]} for convenient value access.
26+
#
27+
# @return [Arrow::Struct] The `i`-th value.
2928
def get_value(i)
3029
Struct.new(self, i)
3130
end
3231

32+
# @overload find_field(index)
33+
# @param index [Integer] The index of the field to be found.
34+
# @return [Arrow::Array, nil]
35+
# The `index`-th field or `nil` for out of range.
36+
#
37+
# @overload find_field(name)
38+
# @param index [String, Symbol] The name of the field to be found.
39+
# @return [Arrow::Array, nil]
40+
# The field that has `name` or `nil` for nonexistent name.
3341
def find_field(index_or_name)
3442
case index_or_name
3543
when String, Symbol

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,16 @@ def method_missing(name, *args, &block)
6464
end
6565
super
6666
end
67+
68+
def ==(other)
69+
other.is_a?(self.class) and
70+
@array == other.array and
71+
@index == other.index
72+
end
73+
74+
protected
75+
def array
76+
@array
77+
end
6778
end
6879
end

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,22 @@ def setup
4949
end
5050

5151
test("#[]") do
52-
notify("TODO: Returns Arrow::Struct instead.")
53-
assert_equal([[true, false], [1, 2]],
54-
[@array[0].to_a, @array[1].to_a])
52+
assert_equal([
53+
Arrow::Struct.new(@array, 0),
54+
Arrow::Struct.new(@array, 1),
55+
],
56+
@array.to_a)
57+
end
58+
59+
test("#get_value") do
60+
assert_equal([
61+
Arrow::Struct.new(@array, 0),
62+
Arrow::Struct.new(@array, 1),
63+
],
64+
[
65+
@array.get_value(0),
66+
@array.get_value(1),
67+
])
5568
end
5669

5770
sub_test_case("#find_field") do

0 commit comments

Comments
 (0)