Skip to content

Instantly share code, notes, and snippets.

@BinaryMuse
Created February 22, 2011 16:12
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 BinaryMuse/31a948c997850a390089 to your computer and use it in GitHub Desktop.
Save BinaryMuse/31a948c997850a390089 to your computer and use it in GitHub Desktop.
[BinaryMuse ~]: irb
ruby-1.9.2-p136 :001 > def split_str(str, len = 10)
ruby-1.9.2-p136 :002?> fragment = /.{#{len}}/
ruby-1.9.2-p136 :003?> str.split(/(\s+)/).map! { |word|
ruby-1.9.2-p136 :004 > (/\s/ === word) ? word : word.gsub(fragment, '\0<wbr />')
ruby-1.9.2-p136 :005?> }.join
ruby-1.9.2-p136 :006?> end
=> nil
ruby-1.9.2-p136 :007 > split_str("Thisisaverylongstring here are some short ones that won't cause wrap ohnoanotherreallylongone")
=> "Thisisaver<wbr />ylongstrin<wbr />g here are some short ones that won't cause wrap ohnoanothe<wbr />rreallylon<wbr />gone"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment