Skip to content

Instantly share code, notes, and snippets.

@ka8725
Last active August 29, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ka8725/11003811 to your computer and use it in GitHub Desktop.
Save ka8725/11003811 to your computer and use it in GitHub Desktop.
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
@ka8725
Copy link
Author

ka8725 commented Apr 17, 2014

require 'minitest/autorun'

BOOLEAN_MAPPINGS = {
  'true' => true,
  'false' => false
}

def typecast_hash_values(hash)
  hash.each do |k, old_value|
    hash[k] &&= hash[k] == 'true'
  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
Run options: --seed 45169

# Running tests:

F

Finished tests in 0.016739s, 59.7407 tests/s, 179.2222 assertions/s.

  1) Failure:
test_typecast_hash_values(TestBooleanHash) [/Users/ka8725/projects/t.rb:25]:
Expected: "string"
  Actual: false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment