Skip to content

Instantly share code, notes, and snippets.

@gravis
Created January 30, 2009 14:12
Show Gist options
  • Save gravis/55081 to your computer and use it in GitHub Desktop.
Save gravis/55081 to your computer and use it in GitHub Desktop.
Convert keys and values to int (to_i) for a Hash
# convert a hash composed of strings to integers
# ex: h = {"1" => "2", "3" => "4"}
# I want {1 => 2, 3 => 4}
string_hash = {"1" => "2", "3" => "4"}
# => {"1"=>"2", "3"=>"4"}
int_hash = Hash[*string_hash.to_a.flatten.map(&:to_i)]
# => {1=>2, 3=>4}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment