On May 6, 2006, at 11:19 AM, Eli Bendersky wrote:

> Hello,
>
> There are two Ruby features I want to combine. One is sub-classes (or
> embedded classes):
>
> class Foo
> 	def initialize(x)
> 		puts "new Foo #{x}"
> 	end
>
> 	class Bar
> 		def initialize(y)
> 			puts "new Bar #{y}"
> 		end
> 	end
> end
>
>
> joe = Foo::Bar.new(12)    # prints "new Bar 12"
>
>
> Another is dynamic creation of new classes using Struct:
>
> Customer = Struct.new(:name, :address)     #=> Customer
> eddy = Customer.new("Dave", "123 Main")
>
> Here 'Customer' is a class and 'eddy' is an instance of the Customer
> class.
>
> Now, by combining the features, I mean creating a subclass of Foo
> dynamically, using Struct. The following pseudo-code demonstates it
> (pseudo because it obviously doesn't work and only shows what I'm  
> trying
> to do):
>
> class Foo
> 	def initialize(x)
> 		puts "new Foo #{x}"
> 	end
>
> 	Bar = Struct.new(:name, :address)
> end
>
> eddy = Foo::Bar.new("Dave", "123 Main")
>
>
> How can this be done ?
>

I'm confused. This works for me, the above code works fine for me.

Putting a class inside another class does not subclass it though (Nor  
is it supposed to, you're just setting a constant for the enclosing  
class).



> TIA
> Eli
>
> -- 
> Posted via http://www.ruby-forum.com/.
>