Reputation: 1923
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
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
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
Reputation: 25029
Fixnum
is the ruby class for standard integers.
Well to be specific, the Integer class covers both Fixnum
s and Bignum
s, but in all honesty there's nothing to done here.
Upvotes: 0