From: Robert Klemme Date: 2005-03-31T16:39:44+09:00 Subject: Re: changing the context in which a block executes "John-Mason P. Shackelford" schrieb im Newsbeitrag news:424B14FE.3020000@shackelford.org... > Greetings. > > I'd like to define a method which accepts a block such callers can write: > > some_method do > op1(arg) > op2(arg) > end > > instead of: > > some_method do |obj| > obj.op1(arg) > obj.op2(arg) > end > > Since within a block self.class.name always returns Object (not the > object of the method which takes the block) it is necessary to use > instance_eval to expose methods available within the context of the > block. This however, breaks encapsulation exposing an object's instance > data and private methods to the block. In Ruby you can *always* directly access an instance's state and thus encapsulation is never really enforced: >> class Foo >> attr_accessor :bar >> end => nil >> f=Foo.new => # >> f.bar = 10 => 10 >> f.instance_variables => ["@bar"] >> f.instance_variable_get "@bar" => 10 So strictly speaking, you cannont prevent someone from accessing instance state. There might be ways but they are usually far too complex to bother to use them. Kind regards robert