Why there is not "replace" method for Fixnum?

  • Thread starter Iñaki Baz Castillo
  • Start date
I

Iñaki Baz Castillo

Hi, using String#replace I can "simulate" a pointer (thanks to David A. for=
=20
the explanation):

a =3D "orignal a value"
b =3D a
b =3D "new value"
a
=3D> "original a value"
b
=3D> "new value"

This is obvious: after the new assigment of "b" it points to a different=20
object.

So I can use "replace":

a =3D "orignal a value"
b =3D a
b.replace "new value"
a
=3D> "new value"
b
=3D> "new value"


But Fixnum has not this method "replace", neither Float. Doesn't make sense=
=20
any primitive Ruby Class having this "replace" method instead of just one o=
f=20
them?






=2D-=20
I=C3=B1aki Baz Castillo
 
S

Sebastian Hungerecker

I=C3=B1aki Baz Castillo said:
But Fixnum has not this method "replace", neither Float.

Consider this:
a =3D 5
b =3D 5
a.object_id =3D=3D b.object_id #=3D> true

So a and b point to the same object even if there was no "a =3D b" anywhere=
=2E=20
That is because in ruby there is only one single instance of any integer, s=
o=20
two variables pointing to 5 always point to the same object. This is becaus=
e=20
of performance. As a consequence of this something like 5.replace 6 would=20
change *all* occurences of 5 to 6 which clearly would not be the desired=20
behaviour. So there is no Integer#replace.

HTH,
Sebastian
=2D-=20
NP: Depeche Mode - Shake The Disease
Jabber: (e-mail address removed)
ICQ: 205544826
 
I

Iñaki Baz Castillo

El S=C3=A1bado, 3 de Mayo de 2008, Sebastian Hungerecker escribi=C3=B3:
Consider this:
a =3D 5
b =3D 5
a.object_id =3D=3D b.object_id #=3D> true

So a and b point to the same object even if there was no "a =3D b" anywhe= re.
That is because in ruby there is only one single instance of any integer,
so two variables pointing to 5 always point to the same object. This is
because of performance. As a consequence of this something like 5.replace= 6
would change *all* occurences of 5 to 6 which clearly would not be the
desired behaviour. So there is no Integer#replace.

Opss, aewsome! I couldn't imagine it.

Thanks a lot for the explanation :)


=2D-=20
I=C3=B1aki Baz Castillo
 
G

Gerardo Santana Gómez Garrido

Hi, using String#replace I can "simulate" a pointer (thanks to David A. f= or
the explanation):

a =3D "orignal a value"
b =3D a
b =3D "new value"
a
=3D> "original a value"
b
=3D> "new value"

This is obvious: after the new assigment of "b" it points to a different
object.

So I can use "replace":

a =3D "orignal a value"
b =3D a
b.replace "new value"
a
=3D> "new value"
b
=3D> "new value"


But Fixnum has not this method "replace", neither Float. Doesn't make se= nse
any primitive Ruby Class having this "replace" method instead of just on= e of
them?

Strings are mutable. Neither Fixnum or Float are; they are immediates.
Which means that you can alter the content of a String object, because
it's actually a pointer to data, while a Fixnum object *is* its
content itself. This is done for performance I think.

--=20
Gerardo Santana
 
R

Rick DeNatale

Hi, using String#replace I can "simulate" a pointer (thanks to David A. f= or
the explanation):

a =3D "orignal a value"
b =3D a
b =3D "new value"
a
=3D> "original a value"
b
=3D> "new value"

This is obvious: after the new assigment of "b" it points to a different
object.

So I can use "replace":

a =3D "orignal a value"
b =3D a
b.replace "new value"
a
=3D> "new value"
b
=3D> "new value"


But Fixnum has not this method "replace", neither Float. Doesn't make se= nse
any primitive Ruby Class having this "replace" method instead of just on= e of
them?

The short answer is NO!, let's look more carefully at what's happening
to see why.

a =3D "original object value"
# This binds the variable a to a string object. The identity of the
# string object can be obtained using the object_id method

a.object_id # =3D> 63930

b =3D a

# This binds b to the identical object to which a is currently bound so:

b.object_id # =3D> 63930

b =3D "new value"
# We've now bound b to a different object.
b.object_id # =3D> 63360
b # =3D> "new value"

# But a is still bound to the first object
a.object_id # =3D> 63930

# Now we re-bind b to the original object
b =3D a
b.object_id # =3D> 63930
b # =3D> "original object value"

# And send replace to that object.
b.replace("new value")
b.object_id # =3D> 63930
b # =3D> "new value"
a.object_id # =3D> 63930
a # =3D> "new value"

# Two points. First, methods (like object_id, and replace) operate on
objects, NOT variables.
# Second, the replace method causes the string to change its contents,
NOT its identity. Some Ruby
# objects are mutable, they have methods which can alter their state,
other objects are immutable, once
# created they can't be changed.

a =3D "a new value"
b =3D "a new value"
a.object_id # =3D> 60490
b.object_id # =3D> 60420

# This illustrates that two strings with the same contents, aren't
necessarily the same object.
a =3D=3D b # =3D> true
a.equal?(b) # =3D> false

# The =3D=3D method compares state, equal? compares identity.

# Some objects, such as Fixnums, Symbols, nil, true, and false have
only one instance for a
# particular state.

a =3D 1
b =3D 1
a.object_id # =3D> 3
b.object_id # =3D> 3

Such objects certainly need to be immutable. If there's only one
instance of the fixnum 42, then you really wouldn't want to change its
value to say 43, or life, the universe, and everything would lose
their meaning.
 
I

Iñaki Baz Castillo

El S=E1bado, 3 de Mayo de 2008, Rick DeNatale escribi=F3:
The short answer is NO!, let's look more carefully at what's happening
to see why.

a =3D "original object value"
# This binds the variable a to a string object. The identity of the
# string object can be obtained using the object_id method

a.object_id # =3D> 63930

b =3D a

# This binds b to the identical object to which a is currently bound so:

b.object_id # =3D> 63930

b =3D "new value"
# We've now bound b to a different object.
b.object_id # =3D> 63360
b # =3D> "new value"

# But a is still bound to the first object
a.object_id # =3D> 63930

# Now we re-bind b to the original object
b =3D a
b.object_id # =3D> 63930
b # =3D> "original object value"

# And send replace to that object.
b.replace("new value")
b.object_id # =3D> 63930
b # =3D> "new value"
a.object_id # =3D> 63930
a # =3D> "new value"

# Two points. First, methods (like object_id, and replace) operate on
objects, NOT variables.
# Second, the replace method causes the string to change its contents,
NOT its identity. Some Ruby
# objects are mutable, they have methods which can alter their state,
other objects are immutable, once
# created they can't be changed.

a =3D "a new value"
b =3D "a new value"
a.object_id # =3D> 60490
b.object_id # =3D> 60420

# This illustrates that two strings with the same contents, aren't
necessarily the same object.
a =3D=3D b # =3D> true
a.equal?(b) # =3D> false

# The =3D=3D method compares state, equal? compares identity.

# Some objects, such as Fixnums, Symbols, nil, true, and false have
only one instance for a
# particular state.

a =3D 1
b =3D 1
a.object_id # =3D> 3
b.object_id # =3D> 3

Such objects certainly need to be immutable. If there's only one
instance of the fixnum 42, then you really wouldn't want to change its
value to say 43, or life, the universe, and everything would lose
their meaning.


I hope explanations like this would appear in some Ruby manual and so :)



=2D-=20
I=F1aki Baz Castillo
 
G

Gerardo Santana Gómez Garrido

El S=E1bado, 3 de Mayo de 2008, Rick DeNatale escribi=F3:





I hope explanations like this would appear in some Ruby manual and so :)

What about the Fixnum class documentation:

http://www.ruby-doc.org/core/classes/Fixnum.html

"Fixnum objects have immediate value. This means that when they are
assigned or passed as parameters, the actual object is passed, rather
than a reference to that object. Assignment does not alias Fixnum
objects. There is effectively only one Fixnum object instance for any
given integer value, so, for example, you cannot add a singleton
method to a Fixnum."

--=20
Gerardo Santana
 
I

Iñaki Baz Castillo

El S=E1bado, 3 de Mayo de 2008, Gerardo Santana G=F3mez Garrido escribi=F3:
o :)

What about the Fixnum class documentation:

http://www.ruby-doc.org/core/classes/Fixnum.html

"Fixnum objects have immediate value. This means that when they are
assigned or passed as parameters, the actual object is passed, rather
than a reference to that object. Assignment does not alias Fixnum
objects. There is effectively only one Fixnum object instance for any
given integer value, so, for example, you cannot add a singleton
method to a Fixnum."

Yes, you are completely right :)

But anyway, I think no newbie will read the doc of a integer since probably=
a=20
newbie will no realize of the class nature of a integer in Ruby, completely=
=20
different of any other language.
That's wahy I suggested to explain all this stuf in a Ruby manual instead o=
f=20
core doc.

Regards.

=2D-=20
I=F1aki Baz Castillo
 
R

Rick DeNatale

El S=E1bado, 3 de Mayo de 2008, Gerardo Santana G=F3mez Garrido escribi= =F3:

Yes, you are completely right :)

But anyway, I think no newbie will read the doc of a integer since proba= bly a
newbie will no realize of the class nature of a integer in Ruby, complet= ely
different of any other language.
That's wahy I suggested to explain all this stuf in a Ruby manual instea= d of
core doc.

Most of the popular expositions of Ruby ("Programming Ruby" a.k.a. the
pickaxe, "Ruby for Rails", "The Ruby Programming language", e.g.) do
explain this.

Blogs are also a good source, may I have the temerity to suggest a
couple of articles germane to Ruby objects and variables:

http://talklikeaduck.denhaven2.com/articles/2006/09/13/on-variables-values-=
and-objects
http://talklikeaduck.denhaven2.com/articles/2008/02/08/whose-variable-is-it=
-anyway

--=20
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/
 
I

Iñaki Baz Castillo

El S=E1bado, 3 de Mayo de 2008, Rick DeNatale escribi=F3:
Most of the popular expositions of Ruby ("Programming Ruby" a.k.a. the
pickaxe, "Ruby for Rails", "The Ruby Programming language", e.g.) do
explain this.

Blogs are also a good source, may I have the temerity to suggest a
couple of articles germane to Ruby objects and variables:

http://talklikeaduck.denhaven2.com/articles/2006/09/13/on-variables-value= s-
it
-anyway

Sure great articles, Ill read them :)


=2D-=20
I=F1aki Baz Castillo
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top