Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #5050 > unrolled thread

Method that mutates object

Started byjason solomon <solomon.jas@gmail.com>
First post2011-05-25 14:22 -0500
Last post2011-05-28 21:06 -0500
Articles 20 on this page of 23 — 11 participants

Back to article view | Back to comp.lang.ruby


Contents

  Method that mutates object jason solomon <solomon.jas@gmail.com> - 2011-05-25 14:22 -0500
    Re: Method that mutates object Stefano Crocco <stefano.crocco@alice.it> - 2011-05-25 14:28 -0500
      Re: Method that mutates object jason solomon <solomon.jas@gmail.com> - 2011-05-25 15:52 -0500
    Re: Method that mutates object Adam Prescott <adam@aprescott.com> - 2011-05-25 14:31 -0500
    Re: Method that mutates object Quintus <sutniuq@gmx.net> - 2011-05-25 15:45 -0500
      Re: Method that mutates object jason solomon <solomon.jas@gmail.com> - 2011-05-25 16:03 -0500
    Re: Method that mutates object jason solomon <solomon.jas@gmail.com> - 2011-05-25 15:59 -0500
      Re: Method that mutates object Brian Candler <b.candler@pobox.com> - 2011-05-25 16:20 -0500
    Re: Method that mutates object "jay s." <solomon.jas@gmail.com> - 2011-05-25 16:36 -0500
      Re: Method that mutates object 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-25 17:00 -0500
    Re: Method that mutates object "jay s." <solomon.jas@gmail.com> - 2011-05-26 08:56 -0500
      Re: Method that mutates object Jeremy Bopp <jeremy@bopp.net> - 2011-05-26 09:43 -0500
        Re: Method that mutates object "jay s." <solomon.jas@gmail.com> - 2011-05-26 10:00 -0500
          Re: Method that mutates object Josh Cheek <josh.cheek@gmail.com> - 2011-05-26 13:12 -0500
    Re: Method that mutates object "jay s." <solomon.jas@gmail.com> - 2011-05-26 10:55 -0500
    Re: Method that mutates object "jay s." <solomon.jas@gmail.com> - 2011-05-26 11:07 -0500
      Re: Method that mutates object Jeremy Bopp <jeremy@bopp.net> - 2011-05-26 11:14 -0500
        Re: Method that mutates object "jay s." <solomon.jas@gmail.com> - 2011-05-26 11:30 -0500
      Re: Method that mutates object Gary Wright <gwtmp01@mac.com> - 2011-05-26 16:34 -0500
        Re: Method that mutates object Adam Prescott <adam@aprescott.com> - 2011-05-27 10:57 -0500
        Re: Method that mutates object Christopher Dicely <cmdicely@gmail.com> - 2011-05-28 13:05 -0500
          Re: Method that mutates object Gary Wright <gwtmp01@mac.com> - 2011-05-28 19:29 -0500
            Re: Method that mutates object Gary Wright <gwtmp01@mac.com> - 2011-05-28 21:06 -0500

Page 1 of 2  [1] 2  Next page →


#5050 — Method that mutates object

Fromjason solomon <solomon.jas@gmail.com>
Date2011-05-25 14:22 -0500
SubjectMethod that mutates object
Message-ID<9004f010954dd5cee76c02c11f61ad58@ruby-forum.com>
Say we want to write a String method called clear that takes a given
string and modifies that string to be equal to "".

Ex.

str = "string"

we call str.clear and we get back "", not just "" printed to the screen,
but the value of str is now "" (mutate the original string).


I understand that the following code would just print "" to the screen
but not modify the actual str object.

class String
  def clear
    ""
  end
end


How would you write a method that actually modifies the str object?

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [next] | [standalone]


#5051

FromStefano Crocco <stefano.crocco@alice.it>
Date2011-05-25 14:28 -0500
Message-ID<4C1A27161AC68A2B@smtp208.alice.it>
In reply to#5050
On Thursday 26 May 2011 04:22:46 jason solomon wrote:
> Say we want to write a String method called clear that takes a given
> string and modifies that string to be equal to "".
> 
> Ex.
> 
> str = "string"
> 
> we call str.clear and we get back "", not just "" printed to the screen,
> but the value of str is now "" (mutate the original string).
> 
> 
> I understand that the following code would just print "" to the screen
> but not modify the actual str object.
> 
> class String
>   def clear
>     ""
>   end
> end
> 
> 
> How would you write a method that actually modifies the str object?

This should do what you want

class String

  def clear
    replace ''
  end

end

Stefano

[toc] | [prev] | [next] | [standalone]


#5054

Fromjason solomon <solomon.jas@gmail.com>
Date2011-05-25 15:52 -0500
Message-ID<6a41212dc5d003930656a60e0b6c671e@ruby-forum.com>
In reply to#5051
Stefano Crocco wrote in post #1001011:
> On Thursday 26 May 2011 04:22:46 jason solomon wrote:
>>
>> How would you write a method that actually modifies the str object?
> This should do what you want
>
> class String
>
>   def clear
>     replace ''
>   end
>
> end
>
> Stefano



Thanks for the reply.  Say we wanted to write our own replace method and 
not use the replace method provided by the String class?

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#5052

FromAdam Prescott <adam@aprescott.com>
Date2011-05-25 14:31 -0500
Message-ID<BANLkTikWMjwotNf9ZOzYx-GuyRroFgE9_Q@mail.gmail.com>
In reply to#5050
[Note:  parts of this message were removed to make it a legal post.]

On Wed, May 25, 2011 at 8:22 PM, jason solomon <solomon.jas@gmail.com>wrote:

> I understand that the following code would just print "" to the screen
> but not modify the actual str object.
>
> class String
>  def clear
>    ""
>  end
> end
>
> How would you write a method that actually modifies the str object?
>

String#replace

s1 = s2 = ""
s2.replace("foo")
s2 #=> "foo"
s1.object_id == s2.object_id

def String
  def fooify
    replace("foo")
  end
end

s = "bar"
s.fooify
s == "foo"

[toc] | [prev] | [next] | [standalone]


#5053

FromQuintus <sutniuq@gmx.net>
Date2011-05-25 15:45 -0500
Message-ID<4DDD6A71.7090502@gmx.net>
In reply to#5050
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Am 25.05.2011 21:22, schrieb jason solomon:
> Say we want to write a String method called clear that takes a given
> string and modifies that string to be equal to "".
> 
> Ex.
> 
> str = "string"
> 
> we call str.clear and we get back "", not just "" printed to the screen,
> but the value of str is now "" (mutate the original string).
> 
> 
> I understand that the following code would just print "" to the screen
> but not modify the actual str object.
> 
> class String
>   def clear
>     ""
>   end
> end
> 
> 
> How would you write a method that actually modifies the str object?
> 

Just in addition to what the others said, the method String#clear
already exists.

=================================================
$ ri String#clear
= String#clear

(from ruby core)
-
------------------------------------------------------------------------------
  string.clear    ->  string

-
------------------------------------------------------------------------------

Makes string empty.

       a = "abcde"
       a.clear    #=> ""
=================================================

And does exactly what you want it to.

Vale,
Marvin
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQEcBAEBAgAGBQJN3WptAAoJELh1XLHFkqhaG5AH/1mAcLBo9eKzpJg3XHjIXXAr
vFOUV9+Ow3hSBZ8Q1KDQrKOVd2QBRkkA2vk7wF7H9KrJCar7wElf0OgJHYmjlEhR
HRnnxtoRgdkRo1GoFyvGr7tRzoKlAgPNC93AQFDm7mZFfO6QMtF5aR4xLgWMIZhM
vRNMkjs71XE1lmxASvFDVgW4SS1wxJnzDe0LQZncjOOPaZHQQM2P3aR9mHL8Or8O
FBvuzqgCptKq5Y69xv/AysLZlRu1ja37J6ggK7LIuLsmT2UaydOc9pVgDaunazrI
ym9IASTm/7vmYRKr3FuA98JifEGAEWC6jkuP0MYcLNkGLdhEzkce04FxHdkfodk=
=5xGw
-----END PGP SIGNATURE-----

[toc] | [prev] | [next] | [standalone]


#5056

Fromjason solomon <solomon.jas@gmail.com>
Date2011-05-25 16:03 -0500
Message-ID<0117d11c85166a7d7a4c8a2ee56fac3c@ruby-forum.com>
In reply to#5053
Marvin Gülker wrote in post #1001033:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Am 25.05.2011 21:22, schrieb jason solomon:
>>
>> How would you write a method that actually modifies the str object?
>>
>
> Just in addition to what the others said, the method String#clear
> already exists.
>
> =================================================
> $ ri String#clear
> = String#clear
>
> (from ruby core)
> -
> ------------------------------------------------------------------------------
>   string.clear    ->  string
>
> -
>
------------------------------------------------------------------------------
>
> Makes string empty.
>
>        a = "abcde"
>        a.clear    #=> ""
> =================================================
>
> And does exactly what you want it to.
>
> Vale,
> Marvin
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.11 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>
> iQEcBAEBAgAGBQJN3WptAAoJELh1XLHFkqhaG5AH/1mAcLBo9eKzpJg3XHjIXXAr
> vFOUV9+Ow3hSBZ8Q1KDQrKOVd2QBRkkA2vk7wF7H9KrJCar7wElf0OgJHYmjlEhR
> HRnnxtoRgdkRo1GoFyvGr7tRzoKlAgPNC93AQFDm7mZFfO6QMtF5aR4xLgWMIZhM
> vRNMkjs71XE1lmxASvFDVgW4SS1wxJnzDe0LQZncjOOPaZHQQM2P3aR9mHL8Or8O
> FBvuzqgCptKq5Y69xv/AysLZlRu1ja37J6ggK7LIuLsmT2UaydOc9pVgDaunazrI
> ym9IASTm/7vmYRKr3FuA98JifEGAEWC6jkuP0MYcLNkGLdhEzkce04FxHdkfodk=
> =5xGw
> -----END PGP SIGNATURE-----





Correct me if I am wrong, String#clear does not exist in Ruby 1.8 and 
earlier.

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#5055

Fromjason solomon <solomon.jas@gmail.com>
Date2011-05-25 15:59 -0500
Message-ID<0cb8bc11c47c8417654064ece3987015@ruby-forum.com>
In reply to#5050
I know this is changing gears a little here, but the previous post
reminded me of this.

Say I want to extend the built in Array class with some useless function
called crazy()

class Array
  def crazy
    ...do something...
  end
end


Correct me if I'm wrong, but I now have a class method for Array.  I
call Array.methods just to make sure that it is there, yet it doesn't
show up, however if I do this:

a = []

and then call a.methods, I can see the crazy() method that we wrote
available.  It looks to me as if I am creating an instance method, but I
would think I would do that like this:

def a.crazy
  ...do something...
end


Does this issue have anything to do with the face that the Array class
is immutable?

I'm a bit confused, any help.  Thanks.

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#5057

FromBrian Candler <b.candler@pobox.com>
Date2011-05-25 16:20 -0500
Message-ID<b74b4d796a579724c7fc150335216999@ruby-forum.com>
In reply to#5055
jay s. wrote in post #1001040:
> Say I want to extend the built in Array class with some useless function
> called crazy()
>
> class Array
>   def crazy
>     ...do something...
>   end
> end
>
>
> Correct me if I'm wrong, but I now have a class method for Array.

You're wrong, so I'll correct you :-) "def" creates instance methods. 
You now have a new instance method on class Array; that is, a method 
which is available to all objects which are instances of Array.

> I
> call Array.methods just to make sure that it is there, yet it doesn't
> show up, however if I do this:
>
> a = []
>
> and then call a.methods, I can see the crazy() method that we wrote
> available.

Yes. To call that method, you'd do "a.crazy"

> It looks to me as if I am creating an instance method, but I
> would think I would do that like this:
>
> def a.crazy
>   ...do something...
> end

That would create a singleton method: a method which belongs only to 
that object.

a = []
def a.crazy
  puts "hello"
end
a.crazy   # works

b = []
b.crazy   # NoMethodError

What you did before was this:

class Array
  def crazy
    ...
  end
end

That defines an instance method on class Array - that is, a method which 
is available to all array objects, even arrays which existed before you 
defined the method. This is the "normal" sort of method you define when 
programming.

Just to close the loop, here's how you do class methods:

def Array.crazy
  puts "wibble"
end

Array.crazy

Now, that syntax may look familiar. In Ruby, classes are objects. You 
are defining a singleton method on the object "Array" (which also 
happens to be an object of class "Class")

So, "class methods" are nothing more than singleton methods, on an 
object of class Class.

> Does this issue have anything to do with the face that the Array class
> is immutable?

No, and in any case the class Array is definitely *not* immutable.

$ irb --simple-prompt
>> a = [1,2]
=> [1, 2]
>> a << 3
=> [1, 2, 3]
>> a
=> [1, 2, 3]

> I'm a bit confused

That does appear to be the case :-) You might want to work through some 
documentation. This one is good:
http://www.ruby-doc.org/docs/ProgrammingRuby/

And continue experimenting within irb of course.

Regards,

Brian.

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#5058

From"jay s." <solomon.jas@gmail.com>
Date2011-05-25 16:36 -0500
Message-ID<30490472d5f5547a9c42e70817b90d89@ruby-forum.com>
In reply to#5050
Thank you for the help, that actually cleared up quite a bit for me.

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#5061

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-05-25 17:00 -0500
Message-ID<77d4bf329a2512671436cd4c039ff44f@ruby-forum.com>
In reply to#5058
jay s. wrote in post #1001051:
> Thank you for the help, that actually cleared up quite a bit for me.
>

Here's some more. This line:

> Array.methods

asks the question, "What methods does Array respond to?"  It does not 
ask, "What methods do the instances of the Array class respond to?" 
Array is a class, and in ruby a class is an object/instance of a class 
called Class.  In ruby, every class is an instance of the Class class, 
and as such, a class object responds to instance methods defined in 
Class(and its superclasses: Module and Object).  The most common 
instance method that class objects call is new().

Now here is the confusing part, Class is a class, and because all 
classes in ruby are objects/instances of the Class class, the Class 
object is an instance of itself.  Presto. lol.  Don't even try to 
understand that--but the logic is consistent.

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#5088

From"jay s." <solomon.jas@gmail.com>
Date2011-05-26 08:56 -0500
Message-ID<83f24a3dcb1393dc972a306e2d05d5ed@ruby-forum.com>
In reply to#5050
Thanks again for all replies as it has helped me to wrap my head around
some of the problems that I've experienced.

Say we wanted to write our own replace method and not use the replace
method provided by the String class?  So we want to write a method that
takes a string object and modifies/mutates that same object and then
returns it, without creating a copy of that object.

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#5090

FromJeremy Bopp <jeremy@bopp.net>
Date2011-05-26 09:43 -0500
Message-ID<4DDE6703.1060909@bopp.net>
In reply to#5088
On 5/26/2011 08:56, jay s. wrote:
> Say we wanted to write our own replace method and not use the replace
> method provided by the String class?  So we want to write a method that
> takes a string object and modifies/mutates that same object and then
> returns it, without creating a copy of that object.

What exactly are you trying to accomplish?  The String class provides
multiple methods to mutate the String instance in various ways, and many
of those methods could be specified in terms of the others, including
replace.  Rather than ask someone to figure out another solution to
which you may respond, "and how do we write our own method_x and not use
the method_x provided by the String class," could you provide some
details about your goals?  What kind of modifications on the String
instance do you want your method to perform?  What methods provided by
the String class are out of bounds (as replace apparently is)?

To me this is sounding a bit like a homework assignment, but maybe it's
not.  In any case, the documentation for the String class is actually
pretty good, so you can probably answer your own question with a little
easy reading:

http://rdoc.info/stdlib/core/1.9.2/String

If I understand what you're really trying to accomplish, the method you
want is definitely listed there.

-Jeremy

[toc] | [prev] | [next] | [standalone]


#5091

From"jay s." <solomon.jas@gmail.com>
Date2011-05-26 10:00 -0500
Message-ID<a143cdf8bd5037a895a94675611b22f9@ruby-forum.com>
In reply to#5090
Jeremy Bopp wrote in post #1001249:
> On 5/26/2011 08:56, jay s. wrote:
>> Say we wanted to write our own replace method and not use the replace
>> method provided by the String class?  So we want to write a method that
>> takes a string object and modifies/mutates that same object and then
>> returns it, without creating a copy of that object.
>
> What exactly are you trying to accomplish?  The String class provides
> multiple methods to mutate the String instance in various ways, and many
> of those methods could be specified in terms of the others, including
> replace.  Rather than ask someone to figure out another solution to
> which you may respond, "and how do we write our own method_x and not use
> the method_x provided by the String class," could you provide some
> details about your goals?  What kind of modifications on the String
> instance do you want your method to perform?  What methods provided by
> the String class are out of bounds (as replace apparently is)?
>
> To me this is sounding a bit like a homework assignment, but maybe it's
> not.  In any case, the documentation for the String class is actually
> pretty good, so you can probably answer your own question with a little
> easy reading:
>
> http://rdoc.info/stdlib/core/1.9.2/String
>
> If I understand what you're really trying to accomplish, the method you
> want is definitely listed there.
>
> -Jeremy


Just for clarification, this is by no means a homework assignment.  I am 
new to Ruby and I'm trying to get a better grasp on how Ruby handles 
mutation.  I actually think at this point I'll try looking at Ruby's 
source code and see how String#replace is implemented.  Maybe I'm just 
over complicating my question.

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#5100

FromJosh Cheek <josh.cheek@gmail.com>
Date2011-05-26 13:12 -0500
Message-ID<BANLkTi=MC8u+NExKpUCFhyaMntiYxokkaA@mail.gmail.com>
In reply to#5091
[Note:  parts of this message were removed to make it a legal post.]

On Thu, May 26, 2011 at 12:41 PM, Chad Perrin <code@apotheon.net> wrote:
> On Fri, May 27, 2011 at 12:00:43AM +0900, jay s. wrote:
>>
>> Just for clarification, this is by no means a homework assignment.  I am
>> new to Ruby and I'm trying to get a better grasp on how Ruby handles
>> mutation.  I actually think at this point I'll try looking at Ruby's
>> source code and see how String#replace is implemented.  Maybe I'm just
>> over complicating my question.
>
> For that, you might want to look at the source for String#replace in the
> Rubinius implementation of Ruby.
>
>    https://github.com/evanphx/rubinius
>
> --
> Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ]
>

Since it took me about 10 minutes to look up, here is the actual link:

https://github.com/evanphx/rubinius/blob/84264ce515d64d2d1c7faf4c8cefa1cb610a4b41/kernel/common/string.rb#L1301-1325

[toc] | [prev] | [next] | [standalone]


#5092

From"jay s." <solomon.jas@gmail.com>
Date2011-05-26 10:55 -0500
Message-ID<ec96357017df42aa34f4bc648de94bd4@ruby-forum.com>
In reply to#5050
/*
 *  call-seq:
 *     str.replace(other_str)   -> str
 *
 *  Replaces the contents and taintedness of <i>str</i> with the
corresponding
 *  values in <i>other_str</i>.
 *
 *     s = "hello"         #=> "hello"
 *     s.replace "world"   #=> "world"
 */

VALUE
rb_str_replace(VALUE str, VALUE str2)
{
    str_modifiable(str);
    if (str == str2) return str;

    StringValue(str2);
    str_discard(str);
    return str_replace(str, str2);
}



So from the looks of it the String#replace method is implemented in C,
and I'm assuming the return value of rb_str_replace is VALUE?

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#5093

From"jay s." <solomon.jas@gmail.com>
Date2011-05-26 11:07 -0500
Message-ID<df56923bf89af890cb21dc192169a0bc@ruby-forum.com>
In reply to#5050
I understand that using String#replace works for the String class, but 
what if you were writing a random method for Integer that takes a number 
and sets it equal to some other number.

Ex.

x = 7

class Integer
  def crazy
    ...set x to 5...
  end
end


now when you call x the value is 5

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#5094

FromJeremy Bopp <jeremy@bopp.net>
Date2011-05-26 11:14 -0500
Message-ID<4DDE7C7A.80608@bopp.net>
In reply to#5093
On 5/26/2011 11:07, jay s. wrote:
> I understand that using String#replace works for the String class, but 
> what if you were writing a random method for Integer that takes a number 
> and sets it equal to some other number.
> 
> Ex.
> 
> x = 7
> 
> class Integer
>   def crazy
>     ...set x to 5...
>   end
> end
> 
> 
> now when you call x the value is 5

Instances of Integer are immutable, so you can't do that.  The
documentation for Fixnum (a descendant of Integer) mentions this in the
Overview section:

http://rdoc.info/stdlib/core/1.9.2/Fixnum

-Jeremy

[toc] | [prev] | [next] | [standalone]


#5096

From"jay s." <solomon.jas@gmail.com>
Date2011-05-26 11:30 -0500
Message-ID<4308372504c349f44c3b25ffb7eea8a4@ruby-forum.com>
In reply to#5094
Jeremy Bopp wrote in post #1001275:
> On 5/26/2011 11:07, jay s. wrote:
>>     ...set x to 5...
>>   end
>> end
>>
>>
>> now when you call x the value is 5
>
> Instances of Integer are immutable, so you can't do that.  The
> documentation for Fixnum (a descendant of Integer) mentions this in the
> Overview section:
>
> http://rdoc.info/stdlib/core/1.9.2/Fixnum
>
> -Jeremy




Thanks.

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#5106

FromGary Wright <gwtmp01@mac.com>
Date2011-05-26 16:34 -0500
Message-ID<95E0A202-C51F-47D2-8DBC-665464E07822@mac.com>
In reply to#5093
On May 26, 2011, at 12:07 PM, jay s. wrote:

> I understand that using String#replace works for the String class, but 
> what if you were writing a random method for Integer that takes a number 
> and sets it equal to some other number.
> 
> Ex.
> 
> x = 7
> 
> class Integer
>  def crazy
>    ...set x to 5...
>  end
> end
> 
> 
> now when you call x the value is 5


This seems like a simple question but it touches on some very deep issues of Ruby's object model.  Understanding the object model is the key to understanding why your question doesn't make sense in Ruby.

Ruby's fixnum objects are not containers for an integer value.  This means that for any particular fixnum object there is no way to alter it so that it no longer behaves like a 7 and instead behaves like a 5, for example.  The integer semantics of a fixnum object are defined by the object's *identity*, not by any state it carries around (i.e. instance variables or state that is hidden from the programmer but maintained by the runtime).

>> x = 5       #=> 5
>> x.object_id #=> 11
>> y = 7       #=> 7
>> y.object_id #=> 15
>> z = 3 + 4   #=> 7
>> z.object_id #=> 15
>> z.eql?(y)   #=> true

In that example you can see that y and z reference the *same* object, which happens to be object 15 in my version of Ruby and which happens to behave like the integer 7.  The Ruby runtime ensures that object 15 always behaves like the integer 7 and that it is the *only* object that behaves like the integer 7.

And yes, you can attach instance variables to fixnum objects:

>> a = 2 #=> 2
>> b = 1 + 1 #=> 2
>> a.instance_variable_set('@english', 'two') #=> "two"
>> b.instance_variable_get('@english') #=> "two"
>> a.object_id #=> 5
>> b.object_id #=> 5

So you can see that within Ruby's object model there is one and only one fixnum object that behaves like the integer 2.  That particular object in MRI Ruby 1.9.2 happens to have an object_id of 5, but that is really just an implementation detail. 

Lots and lots of Rubyists describe fixnum objects as 'immutable', but as shown above with instance variables, that is simply not an accurate description of Ruby's fixnum objects. The characteristic that erroneously gets labeled as 'immutability' is that the semantics of a fixnum object are defined by its *identity* and not by its state.  A consequence of this property is that having a *reference* to a fixnum is all you need to interact with the object. The underlying object doesn't even have to 'exist' within the runtime (i.e. there is no memory allocated for every fixnum object).  The runtime can emulate the behavior of fixnum objects simply by manipulating the *references* without ever instantiating the objects.  See <http://en.wikipedia.org/wiki/Tagged_pointer> for more about how this sort of thing is implemented.  There are several other Ruby classes that have this behavior and tend to be implemented in this way:  nil, true, false from NilClass, TrueClass, and FalseClass respectively and symbols.

Going back to your original question about setting x to a new value.  Another way of interpreting that request is not that you are mutating the object that x references but instead you are rebinding the variable x to a different object. Ruby variables are simply named containers for object references so if you can replace the reference to the fixnum 7 associated with variable x with a reference to the fixnum 5, you will have 'set x to 5', which is exactly what Ruby's assignment operation does.

There are a variety of ways for a Ruby method to gain access to variable bindings that are out of the direct scope of the method but it is generally considered bad form for a method to alter variable bindings outside its scope.  Nevertheless, here is one way to do it:

class Integer
  def assign(name, value, binding_object)
    eval "#{name} = #{value}", binding_object
  end
end

>> x = 5                     #=> 5
>> x.assign('x', 7, binding) #=> 7
>> x                         #=> 7

This sort of use of eval and binding is *not* at all common and probably only comes into play with various debugging or programming tools rather than in the normal course of writing Ruby programs.


Gary Wright

[toc] | [prev] | [next] | [standalone]


#5169

FromAdam Prescott <adam@aprescott.com>
Date2011-05-27 10:57 -0500
Message-ID<BANLkTiniu8TJQwiQrbp2b7P-Wr_ZDd5z=g@mail.gmail.com>
In reply to#5106
[Note:  parts of this message were removed to make it a legal post.]

On 26 May 2011 22:40, "Gary Wright" <gwtmp01@mac.com> wrote:
>
> There are a variety of ways for a Ruby method to gain access to variable
bindings that are out of the direct scope of the method but it is generally
considered bad form for a method to alter variable bindings outside its
scope.  Nevertheless, here is one way to do it:
>
> class Integer
>  def assign(name, value, binding_object)
>    eval "#{name} = #{value}", binding_object
>  end
> end
>
> >> x = 5                     #=> 5
> >> x.assign('x', 7, binding) #=> 7
> >> x                         #=> 7
>
> This sort of use of eval and binding is *not* at all common and probably
only comes into play with various debugging or programming tools rather than
in the normal course of writing Ruby programs.

Not to needlessly self-promote or anything, but I recently wrote about
variables and bindings, so perhaps this is informative:

http://aprescott.com/posts/variables-closures-and-scope

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.ruby


csiph-web