Forum: Ruby is_number function ?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Todd S. (Guest)
on 2006-04-28 01:56
Is there a function in core ruby to determin if a variable is a number
or not?

something like @XX.is_numeric? or is_number(@XX)
Daniel H. (Guest)
on 2006-04-28 02:00
(Received via mailing list)
On Apr 27, 2006, at 11:56 PM, Todd S. wrote:

> Is there a function in core ruby to determin if a variable is a number
> or not?
>
> something like @XX.is_numeric? or is_number(@XX)

1.is_a?(Numeric) # => true

-- Daniel
James G. (Guest)
on 2006-04-28 02:00
(Received via mailing list)
On Apr 27, 2006, at 4:56 PM, Todd S. wrote:

> Is there a function in core ruby to determin if a variable is a number
> or not?
>
> something like @XX.is_numeric? or is_number(@XX)

 >> "hello".is_a? Numeric
=> false
 >> 3.is_a? Numeric
=> true

Hope that helps.

James Edward G. II
Logan C. (Guest)
on 2006-04-28 02:03
(Received via mailing list)
On Apr 27, 2006, at 5:58 PM, Daniel H. wrote:

> 1.is_a?(Numeric) # => true

Or Numeric === 1
Todd S. (Guest)
on 2006-04-28 02:55
Daniel H. wrote:

> 1.is_a?(Numeric) # => true
>
> -- Daniel

Thank you.  I thought it was something to that effect.
Unfortunately it doesn't seem to work for me.


However I find that the above returns:

1.is_a(Numeric)
NoMethodError: undefined method `is_a' for 1:Fixnum
        from (irb):4
        from :0

while, 1.is_a? Numeric does indeed return true.
Wilson B. (Guest)
on 2006-04-28 03:22
(Received via mailing list)
On 4/27/06, Todd S. <removed_email_address@domain.invalid> wrote:
> However I find that the above returns:
>
> 1.is_a(Numeric)
> NoMethodError: undefined method `is_a' for 1:Fixnum
>         from (irb):4
>         from :0
>
> while, 1.is_a? Numeric does indeed return true.
>
>

Don't forget the question mark.
This topic is locked and can not be replied to.