Skip to content

Instantly share code, notes, and snippets.

@Amitesh
Created November 26, 2011 20:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Amitesh/1396253 to your computer and use it in GitHub Desktop.
Save Amitesh/1396253 to your computer and use it in GitHub Desktop.
Word wrap in Ruby
# we can change the <wbr> and <br> as per our requirement.
def wrap_long_string(text, max_width = 20)
if text.blank?
return text
end
sp = text.split(/\n/)
sp.collect!{|s|
s = (s.length < max_width) ? s : s.scan(/.{1,#{max_width}}/).join("<wbr>")
}
return sp.join('<br/>')
end
@rbwendt
Copy link

rbwendt commented Feb 15, 2018

Change /.{1,#{max_width}}/ to /.{1,#{max_width}}\s/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment