[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Why there is not "replace" method for Fixnum?

Iñaki Baz Castillo

5/3/2008 5:40:00 PM

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

11 Answers

Sebastian Hungerecker

5/3/2008 5:57:00 PM

0

I=C3=B1aki Baz Castillo wrote:
> 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: sepp2k@jabber.org
ICQ: 205544826

Iñaki Baz Castillo

5/3/2008 5:59:00 PM

0

El S=C3=A1bado, 3 de Mayo de 2008, Sebastian Hungerecker escribi=C3=B3:
> I=C3=B1aki Baz Castillo wrote:
> > 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" 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

Gerardo Santana Gómez Garrido

5/3/2008 5:59:00 PM

0

On Sat, May 3, 2008 at 12:39 PM, I=F1aki Baz Castillo <ibc@aliax.net> wrote=
:
> 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

Rick DeNatale

5/3/2008 6:35:00 PM

0

On Sat, May 3, 2008 at 1:39 PM, I=F1aki Baz Castillo <ibc@aliax.net> wrote:
> 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.

-----
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Iñaki Baz Castillo

5/3/2008 6:42:00 PM

0

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

Gerardo Santana Gómez Garrido

5/3/2008 6:51:00 PM

0

On Sat, May 3, 2008 at 1:41 PM, I=F1aki Baz Castillo <ibc@aliax.net> wrote:
> 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 s=
o:
> >
> > 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 :)
>

What about the Fixnum class documentation:

http://www.ruby-doc.org/core/classes/F...

"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ñaki Baz Castillo

5/3/2008 7:02:00 PM

0

El S=E1bado, 3 de Mayo de 2008, Gerardo Santana G=F3mez Garrido escribi=F3:
> > =A0I hope explanations like this would appear in some Ruby manual and s=
o :)
>
> What about the Fixnum class documentation:
>
> http://www.ruby-doc.org/core/classes/F...
>
> "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

Rick DeNatale

5/3/2008 7:24:00 PM

0

On Sat, May 3, 2008 at 3:01 PM, I=F1aki Baz Castillo <ibc@aliax.net> wrote:
> El S=E1bado, 3 de Mayo de 2008, Gerardo Santana G=F3mez Garrido escribi=
=F3:
>
> > > I hope explanations like this would appear in some Ruby manual and s=
o :)
> >
> > What about the Fixnum class documentation:
> >
> > http://www.ruby-doc.org/core/classes/F...
> >
> > "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 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.denh...articles/2006/09/13/on-variable...
and-objects
http://talklikeaduck.denh...articles/2008/02/08/whose-varia...
-anyway

--=20
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Iñaki Baz Castillo

5/3/2008 7:35:00 PM

0

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-variab...
s-
>and-objects
> http://talklikeaduck.denhaven2.com/articles/2008/02/08/whose-var...
it
>-anyway

Sure great articles, Ill read them :)


=2D-=20
I=F1aki Baz Castillo

dave

10/29/2009 12:14:00 PM

0

RHF wrote:

>
> -if- You read and study the History of Russia before
> the USSR : Russia has always been on the Front-Line
> of the Christian-v-Islam Conflict all along and across
> it's southern republics and borders.

So if I don't read the history it changes?