Class: String

Inherits:
Object show all
Defined in:
lib/string.rb

Overview

Custom extension to String

Instance Method Summary collapse

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”

Raises:

  • (ArgumentError)


10
11
12
13
# File 'lib/string.rb', line 10

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