From: oinkoink+unet@... (Bret Jolly) Date: 2003-03-18T09:09:13+09:00 Subject: Re: Is there a ruby equivalent of "using" to help with namespaces? Try Rob Partington's 'with.rb' # Copyright 2000, Rob Partington # Released under the GPL class Object attr_reader :curObj @curObj = [] def with(*args) @curObj = [] if curObj == nil if block_given? @curObj.push(args[0]) yield @curObj.pop end end def method_missing(method, *args) methname = method.id2name if @curObj.size if args.size > 0 @curObj[-1].send(methname, args) else @curObj[-1].send(methname) end end end end -------------------->8 snip 8<--------------------------- Example of usage: require "with" class Fish attr_reader :type, :name def initialize(type, name="percy") @type=type @name=name end def moo(*args) return "#{name} (a #{type}) says moo and #{args[0]}\n" end end a=Fish.new("halibut") b=Fish.new("turbot", "martin") c=Fish.new("hake") with (a) { print moo("[default]"); print b.moo("[other object]") } with (b) { print moo("pre-nest") with (c) { print moo("nested") } print moo("post-nest") } -------------------------->8 snip 8<------------------------------- Regards, Bret Bret Jolly (Jou Lii Bair) http://www.rexx.com/~oinkoink/