# Instances in an array

**URL:** <https://www.ruby-forum.com/t/instances-in-an-array/195385>\
**Category:** Rails\
**Created:** [September 22, 2010, 1:49pm UTC](https://www.ruby-forum.com/t/instances-in-an-array/195385 "2010-09-22T13:49:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [September 22, 2010, 1:49pm UTC](https://www.ruby-forum.com/t/instances-in-an-array/195385/1 "2010-09-22T13:49:32Z")

</div>

I’ve got a form that has several checkboxes on it with labels and values  
of A - E. I want to display the most commonly selected values.

I also need to consider the possibility that a user may select equal  
numbers of each value.

What would be the best COA for displaying the results of this form?

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [September 22, 2010, 2:59pm UTC](https://www.ruby-forum.com/t/instances-in-an-array/195385/2 "2010-09-22T14:59:58Z")

</div>

Pale H. wrote:

> I’ve got a form that has several checkboxes on it with labels and values  
> of A - E. I want to display the most commonly selected values.
> 
> I also need to consider the possibility that a user may select equal  
> numbers of each value.
> 
> What would be the best COA for displaying the results of this form?

Put the values into an array or the DB, run calculations, display the  
results. Which part are you having trouble with?

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [September 22, 2010, 3:49pm UTC](https://www.ruby-forum.com/t/instances-in-an-array/195385/3 "2010-09-22T15:49:15Z")

</div>

Marnen Laibow-Koser wrote:

> Pale H. wrote:
> 
> > I’ve got a form that has several checkboxes on it with labels and values  
> > of A - E. I want to display the most commonly selected values.
> > 
> > I also need to consider the possibility that a user may select equal  
> > numbers of each value.
> > 
> > What would be the best COA for displaying the results of this form?
> 
> Put the values into an array or the DB, run calculations, display the  
> results. Which part are you having trouble with?

I’ve got the values into an array. How, then, do I count the number of,  
for example, C values? I thought there would be an array method to  
determine the most commonly occurring values and return them.

> Best,  
> –Â  
> Marnen Laibow-Koser  
> [http://www.marnen.org](http://www.marnen.org)  
> removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [September 22, 2010, 4:09pm UTC](https://www.ruby-forum.com/t/instances-in-an-array/195385/4 "2010-09-22T16:09:08Z")

</div>

On 22 September 2010 14:49, Pale H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Put the values into an array or the DB, run calculations, display the  
> > results. Which part are you having trouble with?
> 
> I’ve got the values into an array. How, then, do I count the number of,  
> for example, C values? I thought there would be an array method to  
> determine the most commonly occurring values and return them.

One way would be something like  
num = array.select { |v| v== ‘C’ }.length

I expect there are better ways.

Colin

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [September 22, 2010, 6:35pm UTC](https://www.ruby-forum.com/t/instances-in-an-array/195385/5 "2010-09-22T18:35:19Z")

</div>

Pale H. wrote:

> Marnen Laibow-Koser wrote:
> 
> > Pale H. wrote:
> > 
> > > I’ve got a form that has several checkboxes on it with labels and values  
> > > of A - E. I want to display the most commonly selected values.
> > > 
> > > I also need to consider the possibility that a user may select equal  
> > > numbers of each value.
> > > 
> > > What would be the best COA for displaying the results of this form?
> > 
> > Put the values into an array or the DB, run calculations, display the  
> > results. Which part are you having trouble with?
> 
> I’ve got the values into an array. How, then, do I count the number of,  
> for example, C values? I thought there would be an array method to  
> determine the most commonly occurring values and return them.

Enumerable#count and #group\_by will probably be helpful here.

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [September 22, 2010, 4:17pm UTC](https://www.ruby-forum.com/t/instances-in-an-array/195385/6 "2010-09-22T16:17:12Z")

</div>

Colin L. wrote:

> On 22 September 2010 14:49, Pale H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > Put the values into an array or the DB, run calculations, display the  
> > > results. ï¿½Which part are you having trouble with?
> > 
> > I’ve got the values into an array. How, then, do I count the number of,  
> > for example, C values? I thought there would be an array method to  
> > determine the most commonly occurring values and return them.
> 
> One way would be something like  
> num = array.select { |v| v== ‘C’ }.length

I, too, expected there to be a better way. I did forget about the select  
method but just read and tried it prior to reading your post. Thanks for  
your help, regardless.

> I expect there are better ways.
> 
> Colin
