From: "David A. Black" Date: 2004-08-24T11:15:26+09:00 Subject: Re: *with* block? Hi -- On Tue, 24 Aug 2004, Daniel Sheppard wrote: > If you're just after configuring the instance variables, you might go > for: > > my_obj = boxWidget.new > my_obj.instance_eval do > @x = 0 > @y = 10 > @width = 100 > @height = 100 > end > > Though doing so will make you code fairly fragile - setting variables in sub(/fairly/,"extremely") :-) There's absolutely no guarantee of a one-to-one correspondence between instance variables and setter/getter methods -- and the way those methods are implemented could even change from one release of a library to another. > this way will bypass the setters, so you can't customize your setting > actions. (I was suprised that instance_eval didn't actually grant the > ability to call methods on the object without prefixing them) It's because it's a parse-time thing: all the bare identifiers on the left of ='s are designated local variables. So #instance_eval is in the same boat as other contexts (instance methods, etc.). Also, its block acts as a closure: a = 1 "".instance_eval { a = 2 } # pointless, but just to illustrate p a # 2 David -- David A. Black dblack@wobblini.net