From: Adam Anderson Date: 2007-11-08T05:04:34+09:00 Subject: Re: instance_eval to evaluate methods inside a block in cont This isn't really what I'm trying to do, however. Your example would mean that I would have to pass a backup to a block everytime. What I want to do is basically impossible unless I can change scoping rules, I think. my_method do another_instance_method("foo") method_outside_of_instance("bar") end which would just be equivalent to my_method do |lol| lol.another_instance_method("foo") method_outside_of_instance("bar") end where the lol object is just an instance of the class which has defined both my_method and another_instance_method. I want any method to implicitly check the scope of the class in which the method is being called and *THEN* check the scope outside the class. This is just an exercise in making code more readable and require less typing, as far I am concerned. If I use instance_eval then all the methods inside then only those instance methods will work. I am pretty much resigned to believing this is impossible. I'm always taking suggestions, though! Thanks, Adam Gary Wright wrote: > On Nov 7, 2007, at 3:58 AM, Adam Anderson wrote: >> From what I am reading it would seem that this is just not a >> possibility, however. > > You could use method_missing in the implicit object to delegate > the message to another object: > > class A > def foo > warn "foo in #{self.inspect}" > end > def backup=(b); @backup = b; end > def method_missing(*args, &block) > if @backup > @backup.send(*args, &block) > else > super > end > end > end > > > class B > def bar > warn "bar in #{self.inspect}" > end > def with(x, &b) > x.backup = self > x.instance_eval &b > end > end > > a = A.new > b = B.new.with(a) { > foo # foo in instance of A > bar # bar in instance of B > } -- Posted via http://www.ruby-forum.com/.