[#3986] Re: Principle of least effort -- another Ruby virtue. — Andrew Hunt <andy@...>

> Principle of Least Effort.

14 messages 2000/07/14

[#4043] What are you using Ruby for? — Dave Thomas <Dave@...>

16 messages 2000/07/16

[#4139] Facilitating Ruby self-propagation with the rig-it autopolymorph application. — Conrad Schneiker <schneik@...>

Hi,

11 messages 2000/07/20

[ruby-talk:03848] Re: Private variables can be changed outside?

From: Dave Thomas <Dave@...>
Date: 2000-07-05 18:58:35 UTC
List: ruby-talk #3848
Aleksi Niemel<aleksi.niemela@cinnober.com> writes:

> Now I managed to drive myself almost insane. I thought private variables
> could not be changed outside class. Anyway, the source code below does
> change.

You can do this a whole lot easier with instance_eval:

   class Private
     def initialize
       @private = "secret"
     end
     def get
      @private
     end
   end

   priv = Private.new
   puts priv.instance_eval { @private }


Ruby doesn't guarantee that you can't poke around inside an object or
class. In fact, quite the reverse--it provides many mechanisms to let
you do just that.

However, I hadn't realized that you could do the trick with
late-binding instance variables. I wonder what mischief that could be
used for.


Thanks


Dave


In This Thread

Prev Next