Created
June 13, 2012 23:00
-
-
Save smsohan/2927054 to your computer and use it in GitHub Desktop.
Ruby example for converting strings into primitive types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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