To_i vs. to_int

Hello,

I have a class which instances can be represented as integers. What is
the difference between the to_i and to_int methods, and when is which
form needed ?

Ico

···

--
:wq
^X^Cy^K^X^C^C^C^C

Ico ha scritto:

Hello,

I have a class which instances can be represented as integers. What is
the difference between the to_i and to_int methods, and when is which
form needed ?

short names (to_x) are for common programmer use.
Long names (to_xyz) are there so that ruby can do some implicit conversions between things that can basically be threated as xyz.

So in your case it could be meaningful to define one and alias the other to it.

Hi --

Ico ha scritto:

Hello,

I have a class which instances can be represented as integers. What is
the difference between the to_i and to_int methods, and when is which
form needed ?

short names (to_x) are for common programmer use.
Long names (to_xyz) are there so that ruby can do some implicit conversions between things that can basically be threated as xyz.

So in your case it could be meaningful to define one and alias the other to it.

What's a use case for to_int? I was thinking of this classic to_str
example:

   class S
     def to_str
       "some text"
     end
   end

   puts "Here is " + S.new # Here is some text

But the to_int equivalent doesn't work:

   class S
     def to_int
       2
     end
   end

   puts 2 + S.new # TypeError: S can't be coerced into Fixnum

Same thing with:

   puts Integer(2) + S.new

I'm stuck trying to find a case where to_int does what to_str does --
but maybe it doesn't.

David

···

On Sun, 30 Jul 2006, gabriele renzi wrote:

--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
   ----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
Ruby for Rails => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.

   class S
     def to_int
       2
     end
   end

   puts 2 + S.new # TypeError: S can't be coerced into Fixnum

      puts 2 | S.new

Guy Decoux

However,

    puts 2 + Integer(S.new) => 4

uses your S#to_int

Regards, Morton

···

On Jul 29, 2006, at 12:58 PM, dblack@wobblini.net wrote:

What's a use case for to_int? I was thinking of this classic to_str
example:

  class S
    def to_str
      "some text"
    end
  end

  puts "Here is " + S.new # Here is some text

But the to_int equivalent doesn't work:

  class S
    def to_int
      2
    end
  end

  puts 2 + S.new # TypeError: S can't be coerced into Fixnum

Same thing with:

  puts Integer(2) + S.new

I'm stuck trying to find a case where to_int does what to_str does --
but maybe it doesn't.

Hi --

···

On Sun, 30 Jul 2006, ts wrote:

> class S
> def to_int
> 2
> end
> end

> puts 2 + S.new # TypeError: S can't be coerced into Fixnum

     puts 2 | S.new

Interesting. What's the rationale for not doing the conversion for +
and - and other operator/methods?

David

--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
   ----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
Ruby for Rails => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.

Hi --

···

On Sun, 30 Jul 2006, Morton Goldberg wrote:

On Jul 29, 2006, at 12:58 PM, dblack@wobblini.net wrote:

What's a use case for to_int? I was thinking of this classic to_str
example:

  class S
    def to_str
      "some text"
    end
  end

  puts "Here is " + S.new # Here is some text

But the to_int equivalent doesn't work:

  class S
    def to_int
      2
    end
  end

  puts 2 + S.new # TypeError: S can't be coerced into Fixnum

Same thing with:

  puts Integer(2) + S.new

I'm stuck trying to find a case where to_int does what to_str does --
but maybe it doesn't.

However,

  puts 2 + Integer(S.new) => 4

uses your S#to_int

True, but it works the same with to_i, as opposed to the string one
where to_s and to_str work differently.

David

--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
   ----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
Ruby for Rails => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.

dblack@wobblini.net ha scritto:

Hi --

> class S
> def to_int
> 2
> end
> end

> puts 2 + S.new # TypeError: S can't be coerced into Fixnum

     puts 2 | S.new

also, Arrays seem to like it:
>> a=Object.new
=> #<Object:0x2c103a0>
>> def a.to_int() 2 end
=> nil
>> [0,1,3][a]
=> 3
>> Array.new(a){ 0 }
=> [0, 0]

Interesting. What's the rationale for not doing the conversion for +
and - and other operator/methods?

I'd like to know the same :slight_smile:

We actually gabbed on this topic a bit recently. See the second half
of the "Symbols are your friends" thread from a couple of days ago.

···

On 7/29/06, gabriele renzi <surrender_itRemove@yahoo.it> wrote:

dblack@wobblini.net ha scritto:
> Hi --
>
>> > class S
>> > def to_int
>> > 2
>> > end
>> > end
>>
>> > puts 2 + S.new # TypeError: S can't be coerced into Fixnum
>>
>> puts 2 | S.new

also, Arrays seem to like it:
>> a=Object.new
=> #<Object:0x2c103a0>
>> def a.to_int() 2 end
=> nil
>> [0,1,3][a]
=> 3
>> Array.new(a){ 0 }
=> [0, 0]

>
> Interesting. What's the rationale for not doing the conversion for +
> and - and other operator/methods?

I'd like to know the same :slight_smile:

Aleks Kissinger ha scritto:

We actually gabbed on this topic a bit recently. See the second half
of the "Symbols are your friends" thread from a couple of days ago.

Oh, I missed this, thank you