Module: Nuggets::String::EvaluateMixin

Included in:
String
Defined in:
lib/nuggets/string/evaluate_mixin.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) evaluate(binding = ::TOPLEVEL_BINDING, filename = nil, lineno = nil)

call-seq:

str.evaluate([binding, [filename, [lineno]]]) => new_str

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.



41
42
43
44
# File 'lib/nuggets/string/evaluate_mixin.rb', line 41

def evaluate(binding = ::TOPLEVEL_BINDING, filename = nil, lineno = nil)
  buffer = gsub(/\\*"/) { |m| "#{"\\" * m.length}#{m}" }
  eval(%Q{"#{buffer}"}, binding, filename || __FILE__, lineno || __LINE__)
end