Posted By

vanne on 05/15/08


Tagged

textmate ruby


Versions (?)



Array divide method


Published in: Other 



Website Promotion
DIRECTORY
is a crucial factor for all websites that need to gain better organic search engine rankings and increase website traffic.
Submitting your website as part of your Web Promotion strategy to our SEO friendly and high traffic Business Directory for review is an excellent way to gain a valuable backlink and increase your websites visibility online.

Submit Site


Expand | Embed | Plain Text
  1. # orginal article : http://blog.jayfields.com/2007/09/ruby-arraychunk.html
  2.  
  3. class Array
  4.  
  5. # divides an array into a smaller chunks
  6. # [1,2,3,4].divide_by 2 # => [[1,2],[3,4]]
  7. def divide_by(number_of_chunks)
  8. chunks = (1..number_of_chunks).collect { [] }
  9. self.each_with_index do |item, index|
  10. chunks[index % number_of_chunks] << item
  11. end
  12. chunks
  13. end
  14.  
  15. alias / divide_by
  16.  
  17. end
  18.  

Report this snippet 

You need to login to post a comment.