From: Zach Dennis Date: 2004-08-24T23:57:03+09:00 Subject: Re: *with* block? Also with the with block being how Jamis Buck and David A. Black posted it works nice with methods as well. def talk puts "i'm talking" end def Test def test puts "i'm testing" end end t = Test.new with t do talk test end 'talk' is called correctly because it is in the current scope of the *with* block and 'test' is called correctly because it exists on object 't'. I think this would be expected behavior as well. If someone was worried about method confusion, they could simply say "self." like David A. Black suggested in one of his original posts on this thread. And it doesn't add complexity to how things work in ruby. It just appears to be a more elegant solution, syntactically. Zach Zach Dennis wrote: > Thanks everyone who replied....I like the instance_eval solution, > although I think it would be nice if *with* were on a Object level, > like Jamis Buck first posted... > > class Object > def with( obj , &block ) > obj.instance_eval &block > end > end > > class Test > def initialize > @x = 5 > end > def x > return x > end > end > > t = Test.new > with t do > puts @x x = 35 > @x = 35 > end > > puts t.x > > I think the *with* block acts as expected as far as instance variables > and local variables are conerned. When you initialize an object you > have to specify the '@' or 'self.' before the variable name. So it > makes sense that in the *block* it would be treated the same. > > I think it is so much cleaner to say: > > with obj do > ... > end > > rather then: > > obj.instance_eval do > > end > > I guess there isn't a huge difference and I might just be being picky, > but I totally think the *with* block is a much cleaner solution. > > Zach >