Module: EleetScript::Interpolatable

Included in:
RegexNode, StringNode
Defined in:
lib/lang/interpreter.rb

Constant Summary collapse

INTERPOLATE_RX =
/[\\]?%(?:@|@@|\$)?[\w]+?(?=\W|$)/

Instance Method Summary collapse

Instance Method Details

#interpolate(str, context) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/lang/interpreter.rb', line 68

def interpolate(str, context)
  new_val = str.dup
  matches = str.scan(INTERPOLATE_RX)
  matches.each do |match|
    next if match.nil? || match == "%" || match == ""
    if match.start_with?("\\")
      next new_val.sub!(match, match[1..-1])
    end
    var = match[1..-1]
    new_val.sub!(match, context[var].call(:to_string).ruby_value)
  end
  new_val
end