O
ole __
Hello again folks,
I'm really starting to like this language. I'm working towards building
a simple expression evaluator. I couldn't seem to find any methods for
testing whether a string contains an integer or float. In PHP (where I'm
from) you've got your is_numeric(). Is there such a method in Ruby?
Anyway, under the assumption that there wasn't and to learn a bit I
decided to add the methods i? and f? to String. I was wondering if I
could get a bit of a critique on this code because I'm still ultra new
to Ruby.
I'm a TDD maniac, so I've got miself a test case in there too:
===============================
class String
# Strict test whether the string is a valid integer
# Underscores are not permitted.
# allowScientific is false by default because "4e3".to_i is 4
def i?(allowScientific = false)
if allowScientific
return (self =~ /^-?\d+(e-?\d+)?$/) != nil
end
(self =~ /^-?\d+$/) != nil
end
# Strict test whether the string is a valid float.
# i.e. 4 is not but 4.0 is. Scientific notation acceptable.
def f?(allowScientific = true)
if allowScientific
return (self =~ /^-?\d*\.\d+(e-?\d+)?$/) != nil
end
(self =~ /^-?\d*.\d+$/) != nil
end
end
require 'test/unit'
class StringExtensionsTest < Test::Unit::TestCase
def testI
assert '45'.i?
assert '-3'.i?
assert !'3s'.i?
assert !'3_000'.i?
assert !'3e3'.i?
end
def testIScientific
assert !'e4'.i?(true)
assert '4e2'.i?(true)
assert '-8e3'.i?(true)
assert '-5e-25'.i?(true)
end
def testFBasic
assert !'3'.f?(false)
assert '3.0'.f?(false)
assert '-25.36'.f?(false)
assert '.52'.f?(false)
assert '-.14'.f?(false)
end
def testFScientific
assert !'e1'.f?
assert !'4e1'.f?
assert '3.12e1'.f?
assert '-2.0e-3'.f?
assert '-.6e2'.f?
end
end
===============================
4 tests, 19 assertions, 0 failures, 0 errors
===============================
Final question, what are your feelings on camelCase and Ruby's apparent
shunning of it?
I'm really starting to like this language. I'm working towards building
a simple expression evaluator. I couldn't seem to find any methods for
testing whether a string contains an integer or float. In PHP (where I'm
from) you've got your is_numeric(). Is there such a method in Ruby?
Anyway, under the assumption that there wasn't and to learn a bit I
decided to add the methods i? and f? to String. I was wondering if I
could get a bit of a critique on this code because I'm still ultra new
to Ruby.
I'm a TDD maniac, so I've got miself a test case in there too:
===============================
class String
# Strict test whether the string is a valid integer
# Underscores are not permitted.
# allowScientific is false by default because "4e3".to_i is 4
def i?(allowScientific = false)
if allowScientific
return (self =~ /^-?\d+(e-?\d+)?$/) != nil
end
(self =~ /^-?\d+$/) != nil
end
# Strict test whether the string is a valid float.
# i.e. 4 is not but 4.0 is. Scientific notation acceptable.
def f?(allowScientific = true)
if allowScientific
return (self =~ /^-?\d*\.\d+(e-?\d+)?$/) != nil
end
(self =~ /^-?\d*.\d+$/) != nil
end
end
require 'test/unit'
class StringExtensionsTest < Test::Unit::TestCase
def testI
assert '45'.i?
assert '-3'.i?
assert !'3s'.i?
assert !'3_000'.i?
assert !'3e3'.i?
end
def testIScientific
assert !'e4'.i?(true)
assert '4e2'.i?(true)
assert '-8e3'.i?(true)
assert '-5e-25'.i?(true)
end
def testFBasic
assert !'3'.f?(false)
assert '3.0'.f?(false)
assert '-25.36'.f?(false)
assert '.52'.f?(false)
assert '-.14'.f?(false)
end
def testFScientific
assert !'e1'.f?
assert !'4e1'.f?
assert '3.12e1'.f?
assert '-2.0e-3'.f?
assert '-.6e2'.f?
end
end
===============================
4 tests, 19 assertions, 0 failures, 0 errors
===============================
Final question, what are your feelings on camelCase and Ruby's apparent
shunning of it?