Created
January 26, 2014 21:50
-
-
Save lodestone/8639912 to your computer and use it in GitHub Desktop.
SquishyI: Squeezes out numbers from a string.
This file contains hidden or 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
irb(main):013:0> "$1,234,567".to_i | |
=> 0 | |
irb(main):014:0> "$1,234,567".to_squishy_i | |
=> 1234567 |
This file contains hidden or 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
module SquishyI | |
def to_squishy_i | |
to_s.scan(/[0-9]/).join.to_i rescue nil | |
end | |
end | |
[Fixnum, String, Float, NilClass].each {|klass| klass.send(:include, SquishyI) } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment