Skip to content

Instantly share code, notes, and snippets.

@brunnogomes
Created August 20, 2015 15:45
Show Gist options
  • Save brunnogomes/67e3fe5ed6ba072dcef5 to your computer and use it in GitHub Desktop.
Save brunnogomes/67e3fe5ed6ba072dcef5 to your computer and use it in GitHub Desktop.
Using Ruby to break text by chunk size, preserving full words (does not break a word in two parts).
text = "Lorem ipsum dolor sit amet, vix agam noluisse inciderint ea, ex augue iriure invidunt nec. Discere epicuri vim in, et delenit molestie adolescens eam. Ei pericula explicari principes nec, solum salutatus intellegam ei sed. Ei vix stet debet vocibus. Pri viris aperiri periculis ei, pri vocibus tacimates id."
text.scan(/.{1,25}\b|.{1,25}/).map(&:strip)
# => ["Lorem ipsum dolor sit", "amet, vix agam noluisse", "inciderint ea, ex augue", "iriure invidunt nec.", "Discere epicuri vim in,", "et delenit molestie", "adolescens eam. Ei", "pericula explicari", "principes nec, solum", "salutatus intellegam ei", "sed. Ei vix stet debet", "vocibus. Pri viris", "aperiri periculis ei, pri", "vocibus tacimates id", "."]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment