lib/footing/extensions/string.rb in footing-0.1.3 vs lib/footing/extensions/string.rb in footing-0.1.4
- old
+ new
@@ -49,7 +49,22 @@
# Indicates if this string represents a boolean value.
def boolean?
!!(self =~ /\A(true|false)\z/i)
end
+ # Casts this string to another datatype.
+ # Supported datatypes:
+ # * Integer
+ # * Float
+ # * Boolean
+ def cast
+ return to_f if numeric? && index(".")
+ return to_i if numeric?
+ if boolean?
+ return true if self =~ /\Atrue\z/i
+ return false if self =~ /\Afalse\z/i
+ end
+ self
+ end
+
end
end