Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Repair numbers converted into string (Rails 4)
repair_nested_params({id: '11', age: '25'}) # Sample
def repair_nested_params(obj)
obj.each { |key, value| obj[key] = parse_value(value) }
end
def parse_value(value)
return repair_nested_params(value) if value.is_a?(Hash)
return value.map(&method(:parse_value)) if value.is_a?(Array)
return value unless value.is_a?(String)
is_numeric = value.match?(/\A[-+]?\d*\.?\d+\z/)
return value unless is_numeric
(value.to_f % 1).positive? ? value.to_f : value.to_i
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment