Skip to content

Instantly share code, notes, and snippets.

@ntalbott
Created February 15, 2011 01:06
Show Gist options
  • Save ntalbott/826909 to your computer and use it in GitHub Desktop.
Save ntalbott/826909 to your computer and use it in GitHub Desktop.
For those still stuck on 1.8, edge cases left as an exercise to the reader...
require 'test/unit'
class TestHashedSprintf < Test::Unit::TestCase
def hashed_sprintf(string, hash)
array = []
cleaned_string = string.gsub(/%<(\w+)>/) do |match|
array << hash[$1.to_sym]
'%'
end
sprintf(cleaned_string, *array)
end
def test_basics
assert_equal(sprintf("%3s %2.1d", "foo", 5), hashed_sprintf("%<code>3s %<total>2.1d", :code => "foo", :total => 5))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment