Skip to content

Instantly share code, notes, and snippets.

@caius
Created November 6, 2018 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caius/c8868cfb37c925c436c4bcd3f81133e4 to your computer and use it in GitHub Desktop.
Save caius/c8868cfb37c925c436c4bcd3f81133e4 to your computer and use it in GitHub Desktop.
module ToInt
def to_int
return unless self
i = to_i
i if self == i.to_s
end
end
String.include(ToInt)
NilClass.include(ToInt)
"5".to_int # => 5
nil.to_int # => nil
"one".to_int # => nil
"something 5 else".to_int # => nil
"50 green apples".to_int # => nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment