Private variables can be changed outside? 
Author Message
 Private variables can be changed outside?

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.

The important routine is hopefully clearly marked:

def change_object_burp_or_fish_burp_outside_fish(text="")

end






accessing. But it's not natural we can access a private variable (of our
child class). If there's no bug in here, this opens up a huge hole. As every
class descents from Object, every class *can* access every other's instance
variables!

This is accomplished by
1) creating a method Object#spy, which access the wanted instance variable
2) a method Class#spy, to the class we wanted to inspect, which caals just
created Object#spy
3) and then by firing a call to Class#spy, which in turn calls Object#spy
with proper 'self', pointing to Class-object.

So we can mess whatever object we want to. Now, if somebody just provides a
method to ask object's instance variables :P.

        - Aleksi

Ps. taking out the comment before 'private' didn't change the run on Ruby
1.4.5.

class Fish
#  private

  public
  def initialize( burp ="blop" )

  end
  def internal_burp

  end
  def change_from_outside
    change_object_burp_or_fish_burp_outside_fish
  end
end

def main_burp

end

def change_object_burp_or_fish_burp_outside_fish(text="")

end

####

fish = Fish.new

fish.internal_burp
main_burp

puts "\nNow we call fish to change itself"
fish.change_from_outside

fish.internal_burp
main_burp

puts "\nNow we call self to change ourselves"
change_object_burp_or_fish_burp_outside_fish(" again")

fish.internal_burp
main_burp

#################
Outputs:
Fish says   'blop'
Object says 'burp'

Now we call fish to change itself
Fish says   'surprise'
Object says 'burp'

Now we call self to change ourselves
Fish says   'surprise'
Object says 'surprise again'



Sun, 22 Dec 2002 03:00:00 GMT  
 Private variables can be changed outside?
Let me add one more thing (I'm starting to feel Columbo ;).

Quote:
> every class *can* access every
> other's instance
> variables!
...
> 2) a method Class#spy, to the class we wanted to inspect,
>    which calls just created Object#spy

Of course it's easier to spy by creating Class#spy like this:

class Foo
  def spy

  end
end

But the trick is to mess up with object's variables without changing it or
letting it know in any way.

If only we know any of the class' methods which calls any of Object's
methods, we could alter Object#called_method, intercept the message, peek
what out cute object has eaten today, call the aliased
Object#original_method and return it's value. The object didn't knew it was
fooled.

Even if I've not made any mistake here somewhere (quite unprobable :) and
such spying is possible, I don't think the hole this opens up is really big.
Or it might be, but I don't know *why* to use it. Anyway, the 'private' has
less power.

        - Aleksi



Sun, 22 Dec 2002 03:00:00 GMT  
 Private variables can be changed outside?

Quote:

> 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

     end
     def get

     end
   end

   priv = Private.new

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



Sun, 22 Dec 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Private variables can be changed outsid e?

2. EQUIVALENCE(variable inside module, variable outside module) ????

3. referring to variables in the scope outside the def

4. variable assignment in process or outside of process

5. Use of variables form outside the class.

6. Changing environment variables into python variables

7. cross-version voodoo: handling 8.3 / 8.4 private structure changes

8. PRIVATE variables (was parameters)

9. Lots of Public and Private variables - no penalties on modern PC.

10. Public\private memory variables

11. Making Instace Variables Private/Local

12. Private instance variables

 

 
Powered by phpBB® Forum Software