Created
November 26, 2011 20:24
-
-
Save Amitesh/1396253 to your computer and use it in GitHub Desktop.
Word wrap in Ruby
This file contains hidden or 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
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Change
/.{1,#{max_width}}/
to/.{1,#{max_width}}\s/