to_i vs. to_int

I

Ico

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
 
G

gabriele renzi

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.
 
D

dblack

Hi --

Ico ha scritto:

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

--
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
http://www.manning.com/black => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.
 
T

ts

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
 
D

dblack

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

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
http://www.manning.com/black => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.
 
M

Morton Goldberg

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
 
D

dblack

A

Aleks Kissinger

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.

(e-mail address removed) ha scritto:

also, Arrays seem to like it:
a=Object.new
=> # said:
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 :)
 
G

gabriele renzi

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top