From: mhm26@... (matt) Date: 2004-06-18T01:33:24+09:00 Subject: Re: Delayed Instantiation - delayed.rb It's just you :-) --------------------------------------------- matt@linux1 matt $ ruby -v ruby 1.8.1 (2004-04-24) [i686-linux-gnu] matt@linux1 matt $ cat moo.rb class DelayedInstance def initialize(klass, *args, &block) @klass = klass @args = args @block = block @object = nil end def method_missing(sym, *args, &block) STDOUT.puts "Inside method missing: #{sym}" @object = @klass.send(:new, *@args, &@block) if @object.nil? @object.send(sym, *args, &block) end end class Test def initialize(*args, &block) STDOUT.puts "Initializing test: #{args.inspect}" @data = args end def show_data STDOUT.puts @data.inspect end def to_s "You don't own space, so stop acting like you do." end end test = DelayedInstance.new(Test, 1, 2, 3, 4) p "to_s" p test.to_s puts p "show_data" p test.show_data matt@linux1 matt $ ruby moo.rb "to_s" "#" "show_data" Inside method missing: show_data Initializing test: [1, 2, 3, 4] [1, 2, 3, 4] nil --------------------------------------------- Anders Borch wrote in message news:<40D17236.8020600@deck.dk>... > is it just me, or could the same results come from this: > > class DelayedInstance > def initialize(klass,*args) > @klass = klass > @args = args > @object = nil > end > > def method_missing(meth,*args,&block) > @object = @klass.send(:new,*@args) if @object.nil? > @object.send(meth,*args,&block) > end > end