From: zuzu Date: 2004-08-24T11:20:44+09:00 Subject: Re: *with* block? On Tue, 24 Aug 2004 11:03:23 +0900, David A. Black wrote: > Hi -- > > > > On Tue, 24 Aug 2004, Zach Dennis wrote: > > > I am looking for a structure like: > > > > my_obj = boxWidget.new > > with my_obj do > > x = 0 > > y = 10 > > width = 100 > > height = 100 > > end > > > > instead of having to say > > > > my_obj = boxWidget.new > > my_obj.x = 0 > > my_obj.y = 10 > > my_obj.width = 100 > > my_obj.height = 100 > > > > > > > > Can you do this already? If not....anyone support an RCR for it besides me? > > Every assignment to a bare identifier (x = 0) is parsed as an > assignment to a local variable. If you want to call a "="-style > method, you have to give the receiver explicitly. So I don't think a > "with" keyword would add much to what you can already do with > instance_eval: > > my_obj = BoxWidget.new > my_obj.instance_eval do > self.x = 0 > self.y = 0 > ... > end > > which then isn't a whole lot better than my_obj.x = 0 and so on. > > Another thing people sometimes do is: > > my_obj = BoxWidget.new do |bw| > bw.x = 0 > bw.y = 10 > ... > end > > which can be nice because it packages the initialization with the > creation of the object, but of course it only works if > BoxWidget#initialize yields the new object. > > David > > -- > David A. Black > dblack@wobblini.net > > my very first thought was of .class_eval, but "obviously" .instance_eval is correct. i find this interesting because this seems to me to be the way to do prototype-based objects rather than class-based objects in ruby; similar to the Self language w/r/t smalltalk. (though offhand i'm not sure how an object would get cloned before doing this modifying.) peace, -z