Why there is not "replace" method for Fixnum?

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

a = “orignal a value”
b = a
b = “new value”
a
=> “original a value”
b
=> “new value”

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

So I can use “replace”:

a = “orignal a value”
b = a
b.replace “new value”
a
=> “new value”
b
=> “new value”

But Fixnum has not this method “replace”, neither Float. Doesn’t make
sense
any primitive Ruby Class having this “replace” method instead of just
one of
them?

Iñaki Baz C. wrote:

But Fixnum has not this method “replace”, neither Float.

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

So a and b point to the same object even if there was no “a = b”
anywhere.
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.

HTH,
Sebastian

El Sábado, 3 de Mayo de 2008, Sebastian H. escribió:

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 :slight_smile:

On Sat, May 3, 2008 at 12:39 PM, Iñaki Baz C. [email protected] wrote:

b
=> “new value”

But Fixnum has not this method “replace”, neither Float. Doesn’t make sense
any primitive Ruby Class having this “replace” method instead of just one 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.

El Sábado, 3 de Mayo de 2008, Rick DeNatale
escribió:

a.object_id # => 63930
a.object_id # => 63930
a = “a new value”

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 :slight_smile:

On Sat, May 3, 2008 at 1:39 PM, Iñaki Baz C. [email protected] wrote:

b
=> “new value”

But Fixnum has not this method “replace”, neither Float. Doesn’t make sense
any primitive Ruby Class having this “replace” method instead of just one of
them?

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

a = “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 # => 63930

b = a

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

b.object_id # => 63930

b = “new value”

We’ve now bound b to a different object.

b.object_id # => 63360
b # => “new value”

But a is still bound to the first object

a.object_id # => 63930

Now we re-bind b to the original object

b = a
b.object_id # => 63930
b # => “original object value”

And send replace to that object.

b.replace(“new value”)
b.object_id # => 63930
b # => “new value”
a.object_id # => 63930
a # => “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 = “a new value”
b = “a new value”
a.object_id # => 60490
b.object_id # => 60420

This illustrates that two strings with the same contents, aren’t

necessarily the same object.
a == b # => true
a.equal?(b) # => false

The == 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 = 1
b = 1
a.object_id # => 3
b.object_id # => 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.denhaven2.com/

On Sat, May 3, 2008 at 1:41 PM, Iñaki Baz C. [email protected] wrote:

a.object_id # => 63930
b # => “new value”
b.replace(“new value”)
other objects are immutable, once
a.equal?(b) # => false
b.object_id # => 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 :slight_smile:

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

On Sat, May 3, 2008 at 3:01 PM, Iñaki Baz C. [email protected] wrote:

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 :slight_smile:

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

Most of the popular expositions of Ruby (“Programming Ruby” a.k.a. the
pickaxe, “Ruby for Rails”, “The Ruby P.ming 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


Rick DeNatale

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

El Sábado, 3 de Mayo de 2008, Gerardo S. Gómez Garrido
escribió:> given integer value, so, for example, you cannot add a singleton

method to a Fixnum."

Yes, you are completely right :slight_smile:

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

Regards.

El Sábado, 3 de Mayo de 2008, Rick DeNatale
escribió:

-anyway
Sure great articles, Ill read them :slight_smile:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs