Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/Getopt/Declare.rb,
lib/Getopt/DelimScanner.rb

Overview

Add some stuff to the String class to allow easy transformation to Regexp and in-place interpolation.

Instance Method Summary collapse

Instance Method Details

#expand_tabs(tabstop = 8) ⇒ Object

Return new string with all tabs set to spaces



52
53
54
55
56
57
58
59
60
# File 'lib/Getopt/Declare.rb', line 52

def expand_tabs( tabstop = 8 )
  h = self.dup
  while h.sub!(/(^|\n)([^\t\n]*)(\t+)/sex) { |f|
	val = ( tabstop * "#$3".length() - ("#$2".length() % tabstop) )
	"#$1#$2" + (" " * val)
    }
  end
  h
end

#expand_tabs!(tabstop = 8) ⇒ Object

Expand all tabs to spaces



42
43
44
45
46
47
48
49
# File 'lib/Getopt/Declare.rb', line 42

def expand_tabs!( tabstop = 8 )
  while self.sub!(/(^|\n)([^\t\n]*)(\t+)/sex) { |f|
	val = ( tabstop * "#$3".length() - ("#$2".length() % tabstop) )
	"#$1#$2" + (" " * val)
    }
  end
  self
end

#interpolate(scope) ⇒ Object

Ideas for String-interpolation stuff courtesy of Hal E. Fulton <[email protected]> via ruby-talk



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/Getopt/DelimScanner.rb', line 47

def interpolate( scope )
    unless scope.is_a?( Binding )
        raise TypeError, "Argument to interpolate must be a Binding, not "\
            "a #{scope.class.name}"
    end

    # $stderr.puts ">>> Interpolating '#{self}'..."

    copy = self.gsub( /"/, %q:\": )
    eval( '"' + copy + '"', scope )
end

#to_re(casefold = false, extended = false) ⇒ Object



40
41
42
# File 'lib/Getopt/DelimScanner.rb', line 40

def to_re( casefold=false, extended=false )
    return Regexp::new( self.dup )
end