Class: Pechkin::Substitute
- Inherits:
-
Object
- Object
- Pechkin::Substitute
- Defined in:
- lib/pechkin/substitute.rb
Overview
Replaces $:varname: patterns inside strings. All posible substitutions are provided through constructor.
Complex templating and text fromatting (like float numbers formatting) is not a goal. We do not aim to implement new templating engine here. Just simple stuff.
Instance Method Summary collapse
-
#initialize(substitutions) ⇒ Substitute
constructor
A new instance of Substitute.
- #process(string) ⇒ Object
Constructor Details
#initialize(substitutions) ⇒ Substitute
Returns a new instance of Substitute.
10 11 12 |
# File 'lib/pechkin/substitute.rb', line 10 def initialize(substitutions) @substitutions = substitutions end |
Instance Method Details
#process(string) ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'lib/pechkin/substitute.rb', line 14 def process(string) string.gsub(/\$\{([A-Za-z0-9_]+)\}/) do |m| key = m[2..-2] value = @substitutions[key] || @substitutions[key.to_sym] (value || m).to_s end end |