Hello --

On Fri, 2 Nov 2001, Matthew Diephouse wrote:

> OK, this will be easier to describe in code.
>
> class A
> 	...
> 	method=(class)
> 		@var = class.new(self)
> 	end
> end
>
> class B
> 	...
> end
>
> class C
> 	...
> end
>
> Ok, basically, I want a method such as A#method that will make a
> variable an object of a class which I'd like to specify as the param.
> This would mean that
> A.method = "B"
> would make @var an object of class B. Is there any way to do this
> besides if..elsif's?

Well, here's one non-if (though possibly iffy :-) way:

  class A
    attr_reader :var
    def cmethod=(klass)
      @var = type.class_eval(klass).new
    end
  end

  class B
  end

  a = A.new
  a.cmethod = "B"
  p a.var         # => #<B:0x4017361c>


David

-- 
David Alan Black
home: dblack / candle.superlink.net
work: blackdav / shu.edu
Web:  http://pirate.shu.edu/~blackdav