Skip to content

Instantly share code, notes, and snippets.

@IronSavior
Created November 18, 2015 20:42
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 IronSavior/bffb99c37d07fc70778b to your computer and use it in GitHub Desktop.
Save IronSavior/bffb99c37d07fc70778b to your computer and use it in GitHub Desktop.
Normalize indents within heredocs so your code stays pretty
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