Last active
August 29, 2015 13:59
-
-
Save ka8725/11003811 to your computer and use it in GitHub Desktop.
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 'minitest/autorun' | |
BOOLEAN_MAPPINGS = { | |
'true' => true, | |
'false' => false | |
} | |
def typecast_hash_values(hash) | |
hash.each do |k, old_value| | |
if (new_value = BOOLEAN_MAPPINGS.fetch(old_value, old_value)) != old_value | |
hash[k] = new_value | |
end | |
end | |
hash | |
end | |
class TestBooleanHash < MiniTest::Unit::TestCase | |
def setup | |
@h = {_true: 'true', _false: 'false', _string: 'string'} | |
end | |
def test_typecast_hash_values | |
typecast_hash_values(@h) | |
assert_equal true, @h[:_true] | |
assert_equal false, @h[:_false] | |
assert_equal 'string', @h[:_string] | |
end | |
end |
Author
ka8725
commented
Apr 17, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment