On 4 Jun 2007, at 15:50, Robert Klemme wrote: > On 04.06.2007 16:29, Eleanor McHugh wrote: >> It's not entirely clear to me that 'instance_eval' would obey the >> same scoping rules as 'with' in VB by dint of introducing a block. > > I'm not too familiar with VB (I'm kinda happy that I could stay > away from it.) so I cannot really comment on the scoping question. > >> I also just happen to prefer the syntactic form in the same way >> that there are people who routinely use 'for' statements in their >> Ruby code. > > Tastes, tastes... Well, you can of course have your "with": > > module Kernel > private > def with(obj, &b) > obj.instance_eval(&b) > end > end > > with "foo" do > puts length > end > # same as > "foo".instance_eval do > puts length > end I must admit that until this thread it had never occurred to me to use instance_eval for this, even though it's bloody obvious the moment you see it written down. I'll probably roll it into a framework I'm currently working on and see how it compares to its VB prototype: it is the one statement I miss from that particular language as it saves so much unnecessary typing and some runtime overhead (classic VB's object model being built on top of COM, the cost of runtime method lookup on deeply nested objects can be prohibitive on older hardware). > PS: Thanks for teaching me another British(?) English idiom - > hadn't known "by dint of" so far. :-) Seems like a fair exchange for the new Ruby idiom :) Ellie Eleanor McHugh Games With Brains ---- raise ArgumentError unless @reality.responds_to? :reason