Method: String#margin

Defined in:
lib/antlr3/test/core-extensions.rb

#margin(n = 0) ⇒ Object

Provides a margin controlled string.

 x = %Q{
       | This
       |   is
       |     margin controlled!
       }.margin

 NOTE: This may still need a bit of tweaking.

CREDIT: Trans


80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/antlr3/test/core-extensions.rb', line 80

def margin( n=0 )
  #d = /\A.*\n\s*(.)/.match( self )[1]
  #d = /\A\s*(.)/.match( self)[1] unless d
  d = ( ( /\A.*\n\s*(.)/.match( self ) ) ||
      ( /\A\s*(.)/.match( self ) ) )[ 1 ]
  return '' unless d
  if n == 0
    gsub( /\n\s*\Z/,'' ).gsub( /^\s*[#{ d }]/, '' )
  else
    gsub( /\n\s*\Z/,'' ).gsub( /^\s*[#{ d }]/, ' ' * n )
  end
end