[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

creating an array by reusing code

Sijo Kg

3/25/2009 10:42:00 AM

Hi
I have
all_req = OnRequest.find(:all, :conditions => [status_id = ?',6])

Now i need to create an array of size 12 containing record counts for
month January to December like [recordcountjanuary,recordcountfeb,....]
So for the first month january I tried like

all_req.select do |r|
r.created_at.mon == 1
end
What i need is reuse the above code I have placed it in a
function But there I have to call it 12 times .How can i avoid this?

Thanks in advance
Sijo
--
Posted via http://www.ruby-....

3 Answers

Jesús Gabriel y Galán

3/25/2009 11:05:00 AM

0

On Wed, Mar 25, 2009 at 11:42 AM, Sijo Kg <sijo@maxxion.com> wrote:
> Hi
> =A0 I have
> all_req =3D OnRequest.find(:all, :conditions =3D> [status_id =3D ?',6])
>
> =A0 Now i need to create an array of size 12 containing record counts for
> month January to December like [recordcountjanuary,recordcountfeb,....]
> So for the first month january I tried like
>
> all_req.select do |r|
> =A0r.created_at.mon =3D=3D 1
> end
> =A0 =A0 =A0 What i need is reuse the above code I have placed it in a
> function But there I have to call it 12 times .How can i avoid this?

Maybe this helps (untested):

recordcount =3D (1..12).map do |month|
all_req.select {|r| r.created_at.mon =3D=3D month}.size
end

Although this does 12 passes through all_req. BTW, if you need the
count you need to check the size of the select. Maybe another approach
could be using a hash:

recordcount =3D Hash.new(0)
all_req.each do |r|
recordcount[r.created_at.mon] +=3D 1
end

Hope this helps,

Jesus.

lasitha

3/25/2009 11:15:00 AM

0

On Wed, Mar 25, 2009 at 4:12 PM, Sijo Kg <sijo@maxxion.com> wrote:
> [...]
> =A0 Now i need to create an array of size 12 containing record counts for
> month January to December like [recordcountjanuary,recordcountfeb,....]
> So for the first month january I tried like
>
> all_req.select do |r|
> =A0r.created_at.mon =3D=3D 1
> end
> =A0 =A0 =A0 What i need is reuse the above code I have placed it in a
> function But there I have to call it 12 times .How can i avoid this?

Let's build this up a step at a time...

monthly_counts =3D (1..12).map do |month|
# call your function here, passing in the month
end

Depending on the scope of all_req, it may just be easier to inline the
function as follows:

monthly_counts =3D (1..12).map do |month|
all_req.select {|r| r.created_at.mon =3D=3D month }.size
end

However, this is inefficient. We have to make 12 passes over the
all_req collection.
You can, instead use Enumerable#group_by:

all_req.group_by {|r| r.created_at.mon }

which will return a hash of items keyed by the month. Getting counts
from this is simply a matter of using #map. I'll leave this as an
exercise :)

Now, having said all that, the most efficient way to do this is
probably to use a group_by clause in your finder. Again, left as an
exercise (mostly 'cos i don't know AR :)

solidarity,
lasitha

Robert Klemme

3/25/2009 4:47:00 PM

0

2009/3/25 lasitha <lasitha.ranatunga@gmail.com>:

> However, this is inefficient. =A0We have to make 12 passes over the
> all_req collection.
> You can, instead use Enumerable#group_by:
>
> all_req.group_by {|r| r.created_at.mon }
>
> which will return a hash of items keyed by the month. =A0Getting counts
> from this is simply a matter of using #map. =A0I'll leave this as an
> exercise :)

Or create a counter Hash and loop once:

counts =3D Hash.new 0
all_req.each {|r| counts[r.created_at.mon] +=3D 1}

> Now, having said all that, the most efficient way to do this is
> probably to use a group_by clause in your finder. =A0Again, left as an
> exercise (mostly 'cos i don't know AR :)

Yep, the database solution is probably the best.

Cheers

robert


--=20
remember.guy do |as, often| as.you_can - without end