The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
ary / 3

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Red Handed

Posts: 1158
Nickname: redhanded
Registered: Dec, 2004

Red Handed is a Ruby-focused group blog.
ary / 3 Posted: Jan 27, 2006 1:17 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Red Handed.
Original Post: ary / 3
Feed Title: RedHanded
Feed URL: http://redhanded.hobix.com/index.xml
Feed Description: sneaking Ruby through the system
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Red Handed
Latest Posts From RedHanded

Advertisement

Sometimes I wish Array.partition could split into more than halves. Something like partition_by or commonality. Like an Array./ method!

 class Array
   def / len
     inject([]) do |ary, x|
       ary << [] if [*ary.last].nitems % len == 0
       ary.last << x
       ary
     end
   end
 end

 >> products = %w[cycles vents hoops willies moogs rifles pools fawns tridents]
 >> products / 3
 => [["cycles", "vents", "hoops"], ["willies", "moogs", "rifles"], 
     ["pools", "fawns", "tridents"]]
 >> products / 2
 => [["cycles", "vents"], ["hoops", "willies"], 
     ["moogs", "rifles"], ["pools", "fawns"], ["tridents"]]

The other alternative being a hopscotch with a flip-flop.

Read: ary / 3

Topic: Kinda like actually going to the gym beats watching people running on TV Previous Topic   Next Topic Topic: Denver-Boulder Ruby User Group

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use