Top
Twitter
Photos
Tuesday
Aug072012

Split a Ruby Enumerable into chunks

Now and again I've had a need to split an array of strings into chunks divided by certain boundary patterns. Kind of like String.split, but for an array, and with multiple boundary patterns. The problem is something akin to splitting the different sections of a MIME multipart message.

In the latest instance, I wrestled with Ruby's take_while and drop_while for a bit, before giving in and kludging together an ugly state machine solution, which I shipped. However, I wasn't happy with it, so I went back and built a better solution.

The Enumerable.split method below will take an arbitrary number of patterns and split the enumerable object into chunks at each of the pattern boundaries.

An example is given below:

I'm not entirely happy with the name split, but it was the best I could come up with at the time.

PrintView Printer Friendly Version

EmailEmail Article to Friend

References (171)

References allow you to track sources for this article, as well as articles that were written in response to this article.
  • Response
    Response: useful reference
    Neat page, Continue the beneficial work. Appreciate it!
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: Full Document
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: Vigrx Plus
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: Tumblr NMN
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: dog aggression
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: NMN VIMEO
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: Chauffeur
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: Chauffeured cars
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: limos perth
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: limo hire perth
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: seo tools
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: care
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: limo hire
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: youtu.be
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: sem
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: selling your home
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: energy consumption
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: visit website
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: security camera
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: haarverlaengerung
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: HELLO FRESH COUPON
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: seamless
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: sneak a peek here
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: barrier pole
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: www.facebook.com
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: bruised eye
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: you could try here
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: Lipitor Attorney
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: Food health
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: balmain
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: define
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: marketing antonyms
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: Best Online Jobs
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: what is marketing
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: sexercise workouts
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: search
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: adaptors
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: commercial
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: fifa 14 Hack
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: director
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: salary
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: relevant web-site
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: act
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: her latest blog
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: article submission
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: ZQuiet Uk
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: discover here
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: limo service
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: Rohde AMMOS
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: sexercise
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: nags head beach
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: lifestyle wear
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: north dandalup
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: cool training
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: www.facebook.com
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: visit this page
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: ZQuiet Malaysia
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: adultconnect.net
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: luxury real estate
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: glenway homes
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: link web page
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: Link Home Page
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: google.com
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: Www.Rebelmouse.com
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: blueberry wine
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: electronics
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: cheap wine online
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: look at these guys
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: paula trang le
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: jimmclean.com
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: www.kcalliance.org
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: buy property
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: www.youtube.com
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: spence diamonds
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: visit this website
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: ZQuiet Review
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: Joe Pantel
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: tretinoin cream
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: forex ea
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    Response: garcinia cambogia
    the inner curmudgeon - Blog - Split a Ruby Enumerable into chunks
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into
  • Response
    Response: Selling Your Home
    If you somehow managed to offer your home today, what might you do first You'd presumably go get a decent domain operator to control, exhort, and enable you to discover a buyer.
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into
  • Response
    the inner curmudgeon - Blog - Split a Ruby Enumerable into
  • Response
    Response: best fish finder
    the inner curmudgeon - Blog - Split a Ruby Enumerable into
  • Response
  • Response
  • Response

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>
« Gender diversity at tech conferences | Main | Adding no-default, non-NULL columns to an existing ActiveRecord model »