# Asking for style critics: Collecting the result of a query

**URL:** <https://www.ruby-forum.com/t/asking-for-style-critics-collecting-the-result-of-a-query/106004>\
**Category:** Ruby\
**Created:** [June 5, 2007, 4:13pm UTC](https://www.ruby-forum.com/t/asking-for-style-critics-collecting-the-result-of-a-query/106004 "2007-06-05T16:13:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [June 5, 2007, 4:13pm UTC](https://www.ruby-forum.com/t/asking-for-style-critics-collecting-the-result-of-a-query/106004/1 "2007-06-05T16:13:24Z")

</div>

May I ask some kind soul to criticize the following piece of code?  
I don’t have that much experience with Ruby yet, but I feel I could  
do it simpler than that.

Problem: Fetch result of a query (it happens to be a SQL query to a  
database, but the essential point here is that I need to generate  
the resulting lines piecewise, each iteration returning the next  
row as an array of values). The result should be returned as a  
matrix (i.e. array of arrays). In practice, the number of rows returned  
will be well below 1000, each row containing around 10 items.

Here is my code:

```
def fetch_result(querystring)
  result=[]
  qres = dbhandle.query(qstr)
  while row = fetch_result.fetch_row # deliver next row
    result.push row # add row to the result array - is this OK?
  end
  result # return result array
end

```

I suspect there might be an overhead in appending to an array  
new rows piecewise as I’m doing it here. Is there a better way  
to do it?

Ronald

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [June 5, 2007, 4:25pm UTC](https://www.ruby-forum.com/t/asking-for-style-critics-collecting-the-result-of-a-query/106004/2 "2007-06-05T16:25:39Z")

</div>

On 05.06.2007 16:12, Ronald F. wrote:

> I suspect there might be an overhead in appending to an array  
> new rows piecewise as I’m doing it here. Is there a better way  
> to do it?
> 
> Ronald

Your sample seems to be missing something: Where does “fetch\_result”  
come into existence? And why don’t you use “qres”?

Attempting to answer your question, if you just append rows without  
modification you might as well do fetch\_result.to\_a.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [June 5, 2007, 5:21pm UTC](https://www.ruby-forum.com/t/asking-for-style-critics-collecting-the-result-of-a-query/106004/3 "2007-06-05T17:21:49Z")

</div>

> ```
> def fetch_result(querystring)
> 
> ```
> 
> way to do it?  
> If the consumer of these data is just going to iterate over the  
> resulting array and do stuff with the rows, you might as well just yield  
> the results and cut out the middleman. It might look something like:

def each\_result(query)  
results = dbh.query(query)  
while row = results.fetch\_row  
yield row  
end  
end

each\_result {|row| puts row.inspect}

Of course, if you need an array for any reason, what you’re doing  
looking fine to me, although doing:

result \<\< row

is more idiomatic ruby, I believe.

- donald

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [June 6, 2007, 10:52am UTC](https://www.ruby-forum.com/t/asking-for-style-critics-collecting-the-result-of-a-query/106004/4 "2007-06-06T10:52:29Z")

</div>

On 06.06.2007 09:44, Ronald F. wrote:

> > come into existence? And why don’t you use “qres”?
> 
> Given my corrections, I guess you would propose
> 
> result = qres.to\_a
> 
> Right? But this would give:
> 
> warning: default `to\_a’ will be obsolete
> 
> and not return the result. The reason is that qres is, in my case, the  
> result object for a MySql query, so I think to\_a is not applicable here.

Hm, it does not implement to\_a properly? I guess then it does not  
extend Enumerable as well. You can do this:

qres.extend(Enumerable).to\_a

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [June 6, 2007, 9:45am UTC](https://www.ruby-forum.com/t/asking-for-style-critics-collecting-the-result-of-a-query/106004/5 "2007-06-06T09:45:19Z")

</div>

> > ```
> > def fetch_result(querystring)
> > result=[]
> > qres = dbhandle.query(qstr)
> > while row = fetch_result.fetch_row # deliver next row
> > result.push row # add row to the result array - is this OK?
> > end
> > result # return result array
> > end
> > 
> > ```

> Your sample seems to be missing something: Where does “fetch\_result”  
> come into existence? And why don’t you use “qres”?

Sorry, my fault when condensing the original code to a form better  
useful for discussing this issue (and during which I also renamed  
variables to make understanding easier in this context). The  
line should say:

while row = qres.fetch\_row …

> Attempting to answer your question, if you just append rows without  
> modification you might as well do fetch\_result.to\_a.

Given my corrections, I guess you would propose

result = qres.to\_a

Right? But this would give:

warning: default `to\_a’ will be obsolete

and not return the result. The reason is that qres is, in my case, the  
result object for a MySql query, so I think to\_a is not applicable here.

Ronald
