Skip to content

Instantly share code, notes, and snippets.

@smsohan
Created June 13, 2012 23:00
Show Gist options
  • Save smsohan/2927054 to your computer and use it in GitHub Desktop.
Save smsohan/2927054 to your computer and use it in GitHub Desktop.
Ruby example for converting strings into primitive types
boolean_converter = ->(s){ s == "true" ? true : false}
type_converters = {Fixnum => ->(s){s.to_i}, Float => ->(s){s.to_f}, Date => ->(s){s.to_date},
TrueClass => boolean_converter, FalseClass => boolean_converter, String => ->(s){s}}
puts type_converters[Fixnum]["100"]
puts type_converters[Float]["100"]
puts type_converters[Date]["100"]
puts type_converters[TrueClass]["true"]
puts type_converters[FalseClass]["true"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment