Skip to content

Instantly share code, notes, and snippets.

@nyarly
Last active December 13, 2015 19:38
Show Gist options
  • Save nyarly/4964167 to your computer and use it in GitHub Desktop.
Save nyarly/4964167 to your computer and use it in GitHub Desktop.
def align(string)
lines = string.split(/\n/)
first = lines.shift
match = /^(\s*)<<</.match(first)
unless(match.nil?)
return lines.map do |line|
unless /^#{match[1]}|^\s*$/ =~ line
raise Errors::UnderIndented, line
end
line.sub(/^#{match[1]}/, "")
end.join("\n")
end
return string
end
Use like:
align(<<-EOS)
<<<
Some left aligned
text
EOS
And know that no matter how the heredoc gets intented, the string that results will be left aligned to the <<<
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment