Created
February 15, 2011 01:06
-
-
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...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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