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

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
mar...@marnen.org

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 Biedenharn          http://agileconsultingllc.com
r...@agileconsultingllc.com



-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-t...@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.


Reply via email to