From: avi@...4.com (Avi Bryant) Date: 2002-06-06T17:42:27+09:00 Subject: Re: Pascal-like 'with' statement? Philip Mak wrote in message news:<20020606002438.GA9947@trapezoid.interserver.net>... > Is there something like Pascal's with statement? I'd like to turn this > code: > > Cfg = OpenStruct.new > Cfg.host = 'localhost' > Cfg.user = 'shoujoai' > Cfg.database = 'shoujoai' > > into something like this: > > Cfg = OpenStruct.new > with Cfg do > host = 'localhost' > user = 'shoujoai' > database = 'shoujoai' > end My favorite solution to this is Smalltalk's cascades: end a method call with a semicolon, and you can chain more calls to the same receiver. So a Ruby equivalent might look like Cfg = OpenStruct.new Cfg.host= 'localhost'; .user= 'shoujoai'; .database= 'shoujoai' Or, in more Smalltalky style, Cfg = OpenStruct.new.host('localhost'); .user('shoujoai'); .database('shoujoai'); .yourself One nice property of this last style is that the whole thing becomes a single expression, suitable for, say, putting in a return statement without even using a local variable. Matz, is there room for this in the parser? ;-)