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
40 def evaluate(binding = ::TOPLEVEL_BINDING, filename = nil, lineno = nil)
41   buffer = gsub(/\\*"/) { |m| "#{"\\" * m.length}#{m}" }
42   eval(%Q{"#{buffer}"}, binding, filename || __FILE__, lineno || __LINE__)
43 end