From: "Hal E. Fulton" Date: 2003-07-16T21:59:28+09:00 Subject: Re: Keyword "with" ----- Original Message ----- From: "Brian Candler" To: "ruby-talk ML" Sent: Wednesday, July 16, 2003 7:03 AM Subject: Re: Keyword "with" > On Wed, Jul 16, 2003 at 08:27:04PM +0900, Mark J. Reed wrote: > > On Wed, Jul 16, 2003 at 06:41:11PM +0900, Mauricio Fern�ndez wrote: > > > > module Kernel > > > > def with(obj, &blk) > > > > obj.instance_eval &blk > > > > end > > > > end > > > > This has apparently been proposed before. The resistance to > > including it may simply come from the fact that, from a general > > design perspective, instance_eval is a Bad Thing(TM). It violates > > encapsulation and removes an object's ability to manage its own data. As regards the encapsulation issue -- it depends on how the real 'with' was implemented. If it were a real language construct and not just a call of instance_eval, then stuff inside the block could be interpreted as calls to accessors and we could disallow direct access to instance vars. Then it just becomes a shorthand -- which is the whole point after all, I think. > Possibly. Also, doing > > def bar > some_long_variable_name.instance_eval { > ... code ... > } > end > > means that 'code' loses access to the instance variables of the enclosing > object, so it's not as useful as you might think. That part can be solved even using the instance_eval version of 'with' -- just pass in arguments as they are needed. A little clunky, of course. def with(obj,*args,&blk) obj.instance_eval &blk end @foo, @bar = 34,45 with(some_obj,@foo,@bar) do |x,y| @foo = x + y end (Note that the block at the end is a special case -- normally a starred argument can only go at the end.) The version of this I'm used to is a trifle different -- I added 'with' to Object so that I could always do obj.with { ... } Same thing, of course. I think a big reason 'with' has not been accepted is that Matz remains unconvinced. It's rather a Pascalism, and I recall he said he never liked Pascal. What's more, I don't think it was ever an RCR. Hal -- Hal Fulton hal9000@hypermetrics.com