Ruby on Rails Friday, May 28, 2010


http://weblog.rubyonrails.org/2006/3/1/new-in-rails-enumerable-group_by-and-array-in_groups_of

enjoy!

This isn't quite the same thing though.

>> a = %w(1 2 3 4 5 6 7 8 9 10)

>> a.in_groups_of(3)
=> [["1", "2", "3"], ["4", "5", "6"], ["7", "8", "9"], ["10", nil, nil]]

It gives you 4 groups, not three as the OP's expecting.

I would use the Array#chunk method at http://blog.jayfields.com/2007/09/ruby-arraychunk.html  like this:

>> a.chunk(3)
=> [["1", "4", "7", "10"], ["2", "5", "8"], ["3", "6", "9"]]

Cheers,


Andy

--
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-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment