Created
November 18, 2015 20:42
-
-
Save IronSavior/bffb99c37d07fc70778b to your computer and use it in GitHub Desktop.
Normalize indents within heredocs so your code stays pretty
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
module NormalizeHeredoc | |
module_function | |
# Normalize indents, like for heredoc strings | |
def NormalizeIndent( msg ) | |
msg = String(msg) | |
depth = msg.scan(/^\s*/).flatten.map(&:size).min | |
msg.gsub(/^\s{#{depth}}/, '') | |
end | |
end | |
module Deep | |
class Nesting | |
include NormalizeHeredoc | |
def demo | |
puts NormalizeIndent(<<-END) | |
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer | |
nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. | |
Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. | |
This block's indent relative to the rest of the message will be | |
preserved. | |
This one too. | |
That is all. | |
END | |
end | |
end | |
end | |
Deep::Nesting.new.demo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment