Test If String Is a Number in Ruby on Rails

Test if string is a number in Ruby on Rails

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

How to test if a string is basically an integer in quotes using Ruby

You can use regular expressions. Here is the function with @janm's suggestions.

class String
def is_i?
!!(self =~ /\A[-+]?[0-9]+\z/)
end
end

An edited version according to comment from @wich:

class String
def is_i?
/\A[-+]?\d+\z/ === self
end
end

In case you only need to check positive numbers

  if !/\A\d+\z/.match(string_to_check)
#Is not a positive number
else
#Is all good ..continue
end

How to check whether a string is an integer in Ruby?

If you're attempting to keep similar semantics to the original post, use either of the following:

"1234223" =~ /\A\d+\z/ ? true : false
#=> true

!!("1234223" =~ /\A\d+\z/)
#=> true

A more idiomatic construction using Ruby 2.4's new Regexp#match? method to return a Boolean result will also do the same thing, while also looking a bit cleaner too. For example:

"1234223".match? /\A\d+\z/
#=> true

How do I determine if a string is numeric?

You could borrow the idea from the NumericalityValidator Rails uses to validate numbers, it uses the Kernel.Float method:

def numeric?(string)
# `!!` converts parsed number to `true`
!!Kernel.Float(string)
rescue TypeError, ArgumentError
false
end

numeric?('1') # => true
numeric?('1.2') # => true
numeric?('.1') # => true
numeric?('a') # => false

It also handles signs, hex numbers, and numbers written in scientific notation:

numeric?('-10')   # => true
numeric?('0xFF') # => true
numeric?('1.2e6') # => true

Check if a string contains only digits in ruby

You can try the following

def check_string(string)
string.scan(/\D/).empty?
end

It would be truthy if string contains only digits or if it is an empty string. Otherwise returns false.

How can I check if a value is a number?

Just regexp it, it's trivial, and not worth thinking about beyond that:

v =~ /\A[-+]?[0-9]*\.?[0-9]+\Z/

(Fixed as per Justin's comment)

Check whether a string contains numbers

If its certain that the format of the valid personname is always

<string>.<number>.<string>

You can try using :[regex, index] method for strings in ruby.

https://ruby-doc.org/core-2.6.1/String.html#method-i-5B-5D

So if

personname = "devid.123.devid"

s[/(.*)(\.\d+\.)(.*)/, 2] = ".123."

There are three different groups in the regex (.*)(\.\d+\.)(.*).

  1. Matches anything
  2. Matches a .<number>.
  3. Matches anything

So based on this regex, the second group should provide you .<number>. which, I hope, is what you need.

Tested with Ruby 2.4.1

Check string is a valid number or not in ruby

I saw such patch:

class String
def is_number?
true if Float(self) rescue false
end
end

if (params[:paramA].blank? || !params[:paramA].is_number?)

Or without the patch:

if (params[:paramA].blank? || (false if Float(params[:paramA]) rescue true))

It supports 12, -12, 12.12, 1e-3 and so on.

Checking if a variable is an integer

You can use the is_a? method

>> 1.is_a? Integer
=> true
>> "dadadad@asdasd.net".is_a? Integer
=> false
>> nil.is_a? Integer
=> false


Related Topics



Leave a reply



Submit