Class: VariableInterpolator
- Inherits:
-
Object
- Object
- VariableInterpolator
- Defined in:
- lib/variable_interpolator.rb
Instance Attribute Summary collapse
-
#variables ⇒ Object
Returns the value of attribute variables.
Instance Method Summary collapse
-
#initialize(variables = {}) ⇒ VariableInterpolator
constructor
A new instance of VariableInterpolator.
- #interpolate(input) ⇒ Object
Constructor Details
#initialize(variables = {}) ⇒ VariableInterpolator
Returns a new instance of VariableInterpolator.
5 6 7 |
# File 'lib/variable_interpolator.rb', line 5 def initialize(variables = {}) @variables = variables.map { |key, value| [key.to_sym, value] }.to_h end |
Instance Attribute Details
#variables ⇒ Object
Returns the value of attribute variables.
3 4 5 |
# File 'lib/variable_interpolator.rb', line 3 def variables @variables end |
Instance Method Details
#interpolate(input) ⇒ Object
9 10 11 12 13 14 |
# File 'lib/variable_interpolator.rb', line 9 def interpolate(input) input.gsub(/\${[a-zA-Z0-9\-_]*}/) do |m| var = m.scan(/[a-zA-Z0-9\-_]+/).first.to_sym variables[var] end end |