[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Asking for style critics: Collecting the result of a query

Ronald Fischer

6/5/2007 2:12:00 PM

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
--
Ronald Fischer <ronald.fischer@venyon.com>
Phone: +49-89-452133-162

3 Answers

Robert Klemme

6/5/2007 2:22:00 PM

0

On 05.06.2007 16:12, Ronald Fischer wrote:
> 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

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

Ronald Fischer

6/6/2007 7:45:00 AM

0

> > 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
--
Ronald Fischer <ronald.fischer@venyon.com>
Phone: +49-89-452133-162


Robert Klemme

6/6/2007 8:50:00 AM

0

On 06.06.2007 09:44, Ronald Fischer wrote:
>>> 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.

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