Searching an array

Hi everyone! :slight_smile:

I’m currently accomplishing what I want, but I’d like to clean it up a
bit. I’ve placed the following items in an array (please excuse my lack
of proper syntax):

@widgets =

[1, red]
[2, blue]
[3, red]
[4, blue]

I’d like to learn a way to search through the @widgets variable an
separate the widgets, so that I could have:

@blue_widgets =

[2, blue]
[4, blue]

@red_widgets =

[1, red]
[4, blue]

…after performing a query on @widgets.

I’m accomplishing this now by doing multiple queries to my database, but
I’m sure there’s a way to do it by just doing one query, and then
separating the data into different variables in my controller. Would
that be the best place to do this?

As always, any help is greatly appreciated.

Steve C. wrote:

@red_widgets =

[1, red]
[4, red]

Fixed the above - should of said “red”.

Steve C. wrote:

Hi everyone! :slight_smile:

I’m currently accomplishing what I want, but I’d like to clean it up a
bit. I’ve placed the following items in an array (please excuse my lack
of proper syntax):

@widgets =

[1, red]
[2, blue]
[3, red]
[4, blue]

I’d like to learn a way to search through the @widgets variable an
separate the widgets, so that I could have:

@blue_widgets =

[2, blue]
[4, blue]

@red_widgets =

[1, red]
[4, blue]

…after performing a query on @widgets.

I’m accomplishing this now by doing multiple queries to my database, but
I’m sure there’s a way to do it by just doing one query, and then
separating the data

Look at Array#partition.

into different variables in my controller. Would
that be the best place to do this?

It might be better in the model. You shouldn’t have much logic in the
controller.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Marnen Laibow-Koser wrote:

Look at Array#partition.

into different variables in my controller. Would
that be the best place to do this?

It might be better in the model. You shouldn’t have much logic in the
controller.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Thanks as always for the guidance, Marnen. I’m checking that out now.

On Jan 20, 2010, at 6:59 PM, Steve C. wrote:

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Thanks as always for the guidance, Marnen. I’m checking that out now.

And if you have more than just red/blue (or any two-choice condition),
you might have more success with Enumerable#group_by (as added by
ActiveSupport). Where Array#partition gives you a two-element array,
Enumerable#group_by gives you a hash with the “condition” as the key
and an array of elements as the value.

irb> require ‘rubygems’
=> true
irb> require ‘activesupport’
=> true
irb> widgets = [ [1, ‘red’], [2, ‘blue’], [3, ‘red’], [4, ‘blue’], ]
=> [[1, “red”], [2, “blue”], [3, “red”], [4, “blue”]]
irb> widgets.partition {|number,color| color == ‘blue’ }
=> [[[2, “blue”], [4, “blue”]], [[1, “red”], [3, “red”]]]
irb> widgets.group_by {|number, color| color }
=> #<OrderedHash {“blue”=>[[2, “blue”], [4, “blue”]], “red”=>[[1,
“red”], [3, “red”]]}>

(OK, so you actually get an OrderedHash from ruby1.8.6, but it works
like you’d expect.)

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs