Created
August 20, 2015 15:45
-
-
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).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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