From: jos@... (Jos Backus) Date: 2002-06-07T08:33:33+09:00 Subject: Re: Returning an instance from a different class from new In article <3CFFC36E.DDC654F9@path.berkeley.edu>, Joel VanderWerf wrote: >Jos Backus wrote: >> >> [For some reason I am no longer subscribed to ruby-talk and I don't get any >> mail back from the ctl address, which is why I have to use my provider's >> crappy newsserver :-(] >> >> How do I make the following return Bar instead of Foo? [clever mods deleted] Thanks, but this depends on the Bar and Baz initializers to not have other arguments. What if I want to pass in extra arguments as in the example below? The way I understand this works is that you override the Foo new class method. Foo.new("1") causes Bar.new to be called with sel == nil, which causes super() (i.e. Foo.new) to be called. super() returns and then Bar.new returns and then Foo.new returns with the new object created by Bar.new. Does that make sense? (Btw, the app is a script to {un,}subscribe myself from different mailing lists run by different types of mailing list managers.) class Foo def initialize(sel, f) @f = f end def self.new(sel=nil) case sel when "1" return Bar.new when "2" return Baz.new when nil super() end end end class Bar < Foo end class Baz < Foo end o = Foo.new("1", "field") p o.type lizzy:~/mail/setup% ruby k k:23:in `new': wrong # of arguments(2 for 1) (ArgumentError) from k:23 -- Jos Backus _/ _/_/_/ Santa Clara, CA _/ _/ _/ _/ _/_/_/ _/ _/ _/ _/ jos@catnook.com _/_/ _/_/_/ use Std::Disclaimer;