Archive for August, 2007

Enumerable::group_by extension

Friday, August 3rd, 2007

I was working recently on a simple RoR website and needed an easy way to display columns of data on the front page. The easiest possible query was a simple find(:all) but after that I wanted to divide the items returned into groups that would be rendered separately. I wasn’t aware of any grouping available in enumerable so I did a bit of a search and ran across this discussion thread. I didn’t read the entire thread (and it’s mention of Set::classify which I was not aware of) but liked the group_by extension it proposed.

module EnumerableExtension
   def group_by(store=Hash.new)
     self.each do |elem|
       group = yield elem
       (store[group] ||= []) << elem
     end
     store
   end
end
Enumerable.send(:include, EnumerableExtension)

controller

@collections = Collection.find(:all).group_by do |collection|
        collection.name.to_sym
end
@left = collections[:left]
@right = collection[:right]

view

<%= render :partial => “left_well”, :locals => {:items => @left} %>
<%= render :partial => “right_well”, :locals => {:items => @right} %>

If I had to do this again I probably would have used Set::classify but I still like the group_by available in Enumerable and I like not being forced to create a set simply to group objects.

Strawberry Lemonade Success

Thursday, August 2nd, 2007

Beth and I took a road trip to Cambridge, Wisconsin last Saturday and Sunday. We have a great time but what was awesome was some fresh strawberry lemonade I made. I used a emeril lagasse recipe I found.

It was very easy to do…from squeezing the lemons to hulling and pureeing the strawberries to mixing everything together I spent about 30-40 minutes on everything. It might take Emeril 5 minutes to make but I was a bit slower with everything.

I added the sparkling water immediately before serving and shook the pitcher very well. I didn’t properly garnish the glasses with mint and a strawberry but it was a road trip so I think Emeril would have understood. Definitely something I will make again!