to_i

Hi   I have string say str1 when i use

res=str1.to_i puts res.class

The result i get is Fixnum. Actually does to_i convert str1 to Integer or Fixnum?So why i did not get result as Integer?

Sijo

Sijo Kg wrote:

Hi   I have string say str1 when i use

res=str1.to_i puts res.class

The result i get is Fixnum. Actually does to_i convert str1 to Integer or Fixnum?So why i did not get result as Integer?

Sijo

Hi Sijo,

    Fixnum is nothing but integer. what you call integer in sql fixnum in ruby.

Thanks Saravanan

Saravanan Krishnan wrote:

    Fixnum is nothing but integer. what you call integer in sql fixnum in ruby.

123.class => Fixnum 12345678901234567890.class => Bignum Fixnum.superclass => Integer Bignum.superclass => Integer

Integer is a class in Ruby with 2 subclasses (Fixnum and Bignum). Fixnum is the class of integers with a small magnitude. These are represented internally using a type your operating system can manipulate. Bignum is the class of all integers with a magnitude too big to fit into that type. Bignum can be used for arbitrary sized integers.

Ruby uses whichever class it needs to behind the scenes to perform arithmetic. You don't need to worry about it because:

123.kind_of? Integer => true 12345678901234567890.kind_of? Integer => true

Integer is the superclass, Fixnum is more appropriate.

Ryan Bigg wrote:

Integer is the superclass, Fixnum is more appropriate.

Great.. Thanks Ryan