Skip to content

Instantly share code, notes, and snippets.

@Amitesh
Created August 21, 2011 07:24
Show Gist options
  • Save Amitesh/1160287 to your computer and use it in GitHub Desktop.
Save Amitesh/1160287 to your computer and use it in GitHub Desktop.
Word wrap in Ruby
# It works for pure text only. It will fail for html and url type of text.
# http://henrik.nyh.se/2007/03/ruby-wordwrap-method
def wrap_long_string(text,max_width = 20)
(text.length < max_width) ?
text :
text.scan(/.{1,#{max_width}}/).join("<wbr>")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment