From: Gavin Kistner Date: 2005-10-30T03:44:49+09:00 Subject: Strip leading indentation [was Re: Iterate over md Array (the Ruby way)] On Oct 16, 2005, at 2:02 PM, James Edward Gray II wrote: > On Oct 16, 2005, at 2:56 PM, Brian Schr�der wrote: >> that is cool. I did not know that it was possible to chain methods to >> a herdoc like this. > > I use that a lot, because I'm very anal and insist on indenting my > HERDOCs. :) Works great: > > data = < indented > lines > go > here > END Here's what I use; it allows for arbitrary indent level, and indented lines within the block: [Sliver:~/Desktop] gkistner$ cat strip_indent.rb class String def strip_indent (new_str = self.dup).strip_indent! new_str end def strip_indent! if leading_indent = self[ /\A\s*^([ \t]+)\S/, 1 ] self.gsub!( /^#{leading_indent}/, '' ) end end end if __FILE__ == $0 puts <<-ENDTEXT.strip_indent It's the end of the world (as we know it) and I feel fine. ENDTEXT end [Sliver:~/Desktop] gkistner$ ruby strip_indent.rb It's the end of the world (as we know it) and I feel fine.