Shruthi R
Shruthi R

Reputation: 1923

How to convert Fixnum to Integer in ruby

I am trying to get Integers, but i am getting 'Fixnum' values.

For Eg:

arr = ["1", "2", "3", "4"]
arr.each do |a|
m = a.to_i
m.class.name

Result
=> Fixnum

According to the above example, how can i get Integer values? Fixnum is a Integer only but while implementing one plugin, it will through an error like 'Please enter only integer'.

Upvotes: 0

Views: 11928

Answers (3)

Arup Rakshit
Arup Rakshit

Reputation: 118261

All Fixnum(s) are already Integer. Here is some sample:

"12".to_i.class
#=> Fixnum
"12".to_i.integer?
#=> true
"12".to_i.to_int
#=> 12

The above all is possible as-

"12".to_i.class.superclass
#=> Integer

Upvotes: 0

js-coder
js-coder

Reputation: 8336

In Ruby integers are either of the class Fixnum or Bignum for bigger numbers. Both of them inherit from the Integer class.

So you already got an integer, no need to convert it further.

1.class #=> Fixnum
1.class.superclass #=> Integer

To convert the array elements to integers you would do this:

arr = ["1", "2", "3", "4"]
arr.map(&:to_i) #=> [1, 2, 3, 4]

Upvotes: 14

sevenseacat
sevenseacat

Reputation: 25029

Fixnum is the ruby class for standard integers.

Well to be specific, the Integer class covers both Fixnums and Bignums, but in all honesty there's nothing to done here.

Upvotes: 0

Related Questions