From: Robert Klemme Date: 2006-05-13T00:10:46+09:00 Subject: Re: An inherent mismatch when creating dynamic classes 2006/5/12, Eli Bendersky : > Class names in Ruby must begin with a capital litter, like constants: > > class poke > end > > ==> ERROR: class/module name must be CONSTANT > > On the other hand, dynamic assignment to constants is illegal: > > def foo > Joe = 5 > p Joe > end > > ==> ERROR: dynamic constant assignment > > So suppose I have some method 'make_class' that returns a dynamically > created class. I can do (outside a method): > > def make_class > Class.new > end > > Bobik = make_class() > > argh = Bobik.new > p argh.class > > ==> Bobik > > Nice... But inside a method / class: > > def make_class > Class.new > end > > def try_it > Bobik = make_class() > end > > ==> ERROR: dynamic constant assignment > > However, I can do: > > def make_class > Class.new > end This method is superfluous. You can simply do "bobik = Class.new" in this method: > def try_it > bobik = make_class() > argh = bobik.new > p argh.class > end > > try_it > > ==> # > > This looks like a mismatch between two unrelated features (true, > assigning to constants dynamically is fishy, but creating new classes > isn't !), right ? I don't see the problem. How can unrelated features mismatch? Also: Constant assignments are simply not allowed in method bodies. > And why the class name 'Bobik' is printed, while # is > printed when the class name is in lowercase. Does the lowercase change > anything semantically ? Uppercase names are constants and this involves some magic. Consider: >> c = Class.new => # >> c.name => "" >> Foo = c => Foo >> c.name => "Foo" Classes do not have a proper name until assigned a constance. But otherwise they are fully functional. No problem with that. Cheers robert -- Have a look: http://www.flickr.com/photos/fussel-foto/