module StringInterpolation
heavily based on Masao Mutoh's gettext String
interpolation extension github.com/mutoh/gettext/blob/f6566738b981fe0952548c421042ad1e0cdfb31e/lib/gettext/core_ext/string.rb
Constants
- INTERPOLATION_PATTERN
Public Class Methods
interpolate(string, values, options = {})
click to toggle source
Replace variables (defined in sprintf syntax) in given string with values from variables hash.
If variable value is not found there are 3 possible strategies (configurable via :value_not_found in third options argument):
-
:raise - raise argument error
-
:ignore - ignore the variable in string (leave as is, do not replace) (DEFAULT)
-
:hide - replace the variable in string with empty string
# File lib/string-interpolation.rb, line 20 def interpolate(string, values, options = {}) options[:not_found_strategy] ||= :ignore raise ArgumentError.new('Interpolation values must be a Hash.') unless values.kind_of?(Hash) interpolate_hash(string, values, options) end
interpolate_hash(string, values, options)
click to toggle source
# File lib/string-interpolation.rb, line 26 def interpolate_hash(string, values, options) string.gsub(INTERPOLATION_PATTERN) do |match| if match == '%%' '%' else key = ($1 || $2).to_sym if values.key?(key) value = values[key] value = value.call(values) if value.respond_to?(:call) $3 ? sprintf("%#{$3}", value) : value else if options[:not_found_strategy] == :raise raise ArgumentError.new("missing interpolation argument #{key} in #{string.inspect}. Given values are: (#{values.inspect})") elsif options[:not_found_strategy] == :hide value = '' else value = $& end end end end end