Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/string.rb
Overview
Custom extension to String
Instance Method Summary collapse
-
#sprinth(hash = {}) ⇒ Object
Interpolates hash values into a formatted string.
Instance Method Details
#sprinth(hash = {}) ⇒ Object
Interpolates hash values into a formatted string. s = “Stats uid %uid$-5d belongs to %userid$s” h = 501, ‘userid’: ‘richard s.sprinth(h) # intermediate => “Stats uid %-5d belongs to %s” % [501, ’richard’] # finally => “Stats uid 501 belongs to richard”
9 10 11 12 |
# File 'lib/string.rb', line 9 def sprinth(hash={}) raise ArgumentError.new("sprinth argument must be a Hash") unless hash.is_a?(Hash) self.gsub(/\%\w+\$/,"%") % self.scan(/\%\w+\$/).collect { |token| hash[token[1..-2]] } end |