Skip to content

Instantly share code, notes, and snippets.

@benphelps
Created November 12, 2012 10:11
Show Gist options
  • Save benphelps/4058483 to your computer and use it in GitHub Desktop.
Save benphelps/4058483 to your computer and use it in GitHub Desktop.
1.9.3 (main):0 > a
=> [
[0] 1,
[1] "1",
[2] "1.1",
[3] "a",
[4] "-1",
[5] "-2.2"
]
1.9.3 (main):0 > b
=> {
:a => 1,
:b => "1",
:c => "1.1",
:d => "a",
:e => "-1",
:f => "-2.2"
}
1.9.3 (main):0 > numberize a
=> [
[0] 1,
[1] 1,
[2] 1.1,
[3] "a",
[4] -1,
[5] -2.2
]
1.9.3 (main):0 > numberize b
=> {
:a => 1,
:b => 1,
:c => 1.1,
:d => "a",
:e => -1,
:f => -2.2
}
1.9.3 (main):0 >
def numberize(q)
def convert(s)
if s =~ /^-?\d+$/m
s.to_i
elsif s =~ /^[-+]?\d+\.?\d+$/m
s.to_f
else
s
end
end
if q.class == Hash
q.each do |key, value|
q[key] = convert(value)
end
elsif q.class == Array
q.map! { |s|
convert(s)
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment