From: "David A. Black" Date: 2004-08-24T12:06:48+09:00 Subject: Re: *with* block? Hi -- On Tue, 24 Aug 2004, zuzu wrote: > On Tue, 24 Aug 2004 11:03:23 +0900, David A. Black wrote: > > 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. > > 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.) There's not really much object modifying here, compared to what you *can* do :-) (adding methods to individual objects, etc.) In general I tend to think of Ruby objects as not very class-based: once an object has been launched into objectspace by its class, it follows a path of its own, in which the class of origin may play a role but which is not constrained by it. (And of course Class objects themselves can change.) David -- David A. Black dblack@wobblini.net