0

Two dimensional array has the element which is CSV::Row object like this.

[[csv_row_object]]

I tried to flatten this array and got different result in ruby 2.3 and 2.6.

why different result?

# in Ruby 2.6(ruby 2.6.0rc1)

[[#<CSV::Row "no":"001111" "status":"1" >]].flatten

returns ["no","001111","status","1"]


# in Ruby 2.3.8

[[#<CSV::Row "no":"001111" "status":"1">]].flatten

returns [#<CSV::Row "no":"001111" "status":"1">]
Kouta Osabe
  • 121
  • 1
  • 10
  • 1
    Can you post the code you're using to load the CSV? I don't see this behavior when calling `CSV.read` with `headers: true` – Jay Dorsey Jun 29 '19 at 23:50
  • I don't think your first result could possibly be correct. After all, if `obj` is not an array `[[obj]].flatten #=> [obj]`, regardless of whether `obj` is a CSV row object or something else. – Cary Swoveland Jun 30 '19 at 00:01
  • In ruby 2.6, [[#]].flatten(1) returns [#]. CSV::Row object is not an array.so i expect that [[#]].flatten returns [#] but doesn't. – Kouta Osabe Jun 30 '19 at 01:17
  • I reproduced the first result (v2.6.0rc1) using v2.7.0. I would think the second result (v2.3.8) is correct and the first result is due to a bug, but I can't mind-read the Ruby monks' intent. I did notice that the C code for `Array#flatten` differs between the two versions, but I'm not quite up to digging though it. I'll look at it tomorrow and at a minimum post some example data and code, and some thoughts on the subject. Interesting. – Cary Swoveland Jun 30 '19 at 06:23
  • I reported bug issue below. https://bugs.ruby-lang.org/issues/15967 This is Known issue. Kouhei Sutou said "It's introduced by #10013. Adding CSV::Row#to_ary may be wrong. But I couldn't remove CSV::Row#to_ary to avoid compatibility breakage again." – Kouta Osabe Jun 30 '19 at 22:50

0 Answers0