Weird Array.flatten behaviour with DBI

Hello, I’m having a weird issue with the Array.flatten method when using
it with DBI. The problem is that it doesn’t seem to flatten out results
returned from sth.fetch_all. I rolled my own flatten, but I’d rather
use Ruby’s builtin flatten. Here’s the code I’m working on:

require ‘dbi’

class Test
def initialize
@lims = DBI.connect(“DBI:Mysql:lims”, “myuser”, “mypass”)
end

def flatten(list)
(!list) ? :
((list[0].type == Array) ? flatten(list[0]) : list[0].to_a) +
flatten(list[1, list.length])
end

def get_client(abatno)
@lims.prepare(“select distinct SA_CLTNO from sample where SA_ABATNO =
‘#{abatno}’”) do |sth|
sth.execute
row = sth.fetch_all
rflat = row.flatten
mflat = flatten(row)
puts row.inspect
puts rflat.inspect
puts mflat.inspect
end
end
end

t = Test.new
t.get_client(‘54230’)

-------------------------

output

[[“213”], [“476”]] # row.inspect
[[“213”], [“476”]] # rflat
[“213”, “476”] # mflat

As you can see, the row.flatten method, isn’t flattening the array. I’m quite
confused by this because if I go into irb and do:

[[“213”], [“476”]].flatten

I get

[“213”, “476”]

Here’s the result straight out of mysql, FYI.

···

select distinct SA_CLTNO from sample where SA_ABATNO = ‘54230’;
±---------+
SA_CLTNO |
±---------+
213 |
476 |
±---------+
2 rows in set (0.01 sec)

Any help would be appreciated.

Travis Whitton whitton@atlantic.net

Travis Whitton wrote:

Hello, I’m having a weird issue with the Array.flatten method when using
it with DBI. The problem is that it doesn’t seem to flatten out results
returned from sth.fetch_all. I rolled my own flatten, but I’d rather
use Ruby’s builtin flatten. Here’s the code I’m working on:

require ‘dbi’

class Test
def initialize
@lims = DBI.connect(“DBI:Mysql:lims”, “myuser”, “mypass”)
end

def flatten(list)
(!list) ? :
((list[0].type == Array) ? flatten(list[0]) : list[0].to_a) +

Method fetch_all returns DBI::Row objects, not Arrays!
Use to_a to convert a DBI::Row into an array.

Regards,

Michael