module Nuggets::String::EvaluateMixin

Public Instance Methods

evaluate([binding, [filename, [lineno]]]) → new_str click to toggle source

Basically turns Kernel#eval into an instance method of String – inspired by Ruby Cookbook example 1.3. This allows to pre-populate strings with substitution expressions "#{...}" that can get evaluated in a different environment (= binding) at a later point.

Passes optional arguments filename and lineno on to Kernel#eval.

# File lib/nuggets/string/evaluate_mixin.rb, line 40
def evaluate(binding = ::TOPLEVEL_BINDING, filename = nil, lineno = nil)
  buffer = gsub(/\*"/) { |m| "#{"\\" * m.length}#{m}" }
  eval(%Q{"#{buffer}"}, binding, filename || __FILE__, lineno || __LINE__)
end