[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

to_i vs. to_int

usenet

7/29/2006 4:00:00 PM

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
10 Answers

gabriele renzi

7/29/2006 4:42:00 PM

0

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.

dblack

7/29/2006 4:58:00 PM

0

ts

7/29/2006 5:06:00 PM

0

>>>>> "d" == dblack <dblack@wobblini.net> writes:

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

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

puts 2 | S.new


Guy Decoux

dblack

7/29/2006 5:13:00 PM

0

Morton Goldberg

7/29/2006 5:30:00 PM

0

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

Regards, Morton



dblack

7/29/2006 5:39:00 PM

0

gabriele renzi

7/29/2006 8:49:00 PM

0

dblack@wobblini.net ha scritto:
> Hi --
>
>> d> class S
>> d> def to_int
>> d> 2
>> d> end
>> d> end
>>
>> d> 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 :)

Aleks Kissinger

7/30/2006 8:36:00 AM

0

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 --
> >
> >> d> class S
> >> d> def to_int
> >> d> 2
> >> d> end
> >> d> end
> >>
> >> d> 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 :)
>
>

gabriele renzi

7/30/2006 8:48:00 AM

0

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

Spike Lee

9/19/2008 11:17:00 AM

0

On Sep 19, 2:42 am, "Obama-Biden in 08" <NoTha...@NoSpam.com> wrote:
> "James Of Tucson" <james0tuc...@gmail.com> wrote in messagenews:994b6e03-2138-4031-a15a-9036ad574755@s28g2000prd.googlegroups.com...
>
> > On Sep 18, 5:12 pm, "Obama-Biden in 08" <NoTha...@NoSpam.com> wrote:
>
> > The difference between Festus and the person who makes $2 million
> > in taxable income, is that Festus is actually thinking about the rich guy.
>
> What makes his inability to understand numbers and associated charts and
> graphs is the fact that he claims to have a 5 year Math degree.  I think all
> five years were spent in the 2nd grade.  As an adult.
>
One doesn't need to understand graphs and charts, simply look at the
state of housing, the airlines, the auto industry, employment, the
war, the stock market, gas prices, and health care. Then vote the
republican party out of office....