Create is_number?
Method.
Create a helper method:
def is_number? string
true if Float(string) rescue false
end
And then call it like this:
my_string = '12.34'
is_number?( my_string )
# => true
Extend String
Class.
If you want to be able to call is_number?
directly on the string instead of passing it as a param to your helper function, then you need to define is_number?
as an extension of the String
class, like so:
class String
def is_number?
true if Float(self) rescue false
end
end
And then you can call it with:
my_string.is_number?
# => true
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…