Last active
September 6, 2015 06:34
-
-
Save headquarters/96bbc01de997b9885e85 to your computer and use it in GitHub Desktop.
Prevent widows in Ruby with this simple function. It replaces only the last space character with a non-breaking space in a string.
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
def prevent_widows(text) | |
if text.kind_of? String | |
trimmed_text = text.rstrip | |
last_space_index = trimmed_text.rindex(" ") | |
if !last_space_index.nil? | |
text[last_space_index] = " " | |
end | |
end | |
text | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I guess it would be more appropriately called "prevent orphans", since it's concerned with the last word not being on a line by itself.