Module: Arrow::StringExtensions

Included in:
String
Defined in:
lib/arrow/monkeypatches.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

#interpolate(scope) ⇒ Object

Interpolate any ‘#…’ placeholders in the string within the given scope (a Binding object).



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/arrow/monkeypatches.rb', line 55

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 )
	rescue Exception => err
		nicetrace = err.backtrace.find_all {|frame|
			/in `(interpolate|eval)'/i !~ frame
		}
		Kernel.raise( err, err.message, nicetrace )
end

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

Return the receiving String as a Regexp.



45
46
47
# File 'lib/arrow/monkeypatches.rb', line 45

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