# An inherent mismatch when creating dynamic classes

**URL:** <https://www.ruby-forum.com/t/an-inherent-mismatch-when-creating-dynamic-classes/60359>\
**Category:** Ruby\
**Created:** [May 12, 2006, 4:38pm UTC](https://www.ruby-forum.com/t/an-inherent-mismatch-when-creating-dynamic-classes/60359 "2006-05-12T16:38:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![eliben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3bc359/32.png) [@eliben](https://www.ruby-forum.com/u/eliben)\
**Post date:** [May 12, 2006, 4:38pm UTC](https://www.ruby-forum.com/t/an-inherent-mismatch-when-creating-dynamic-classes/60359/1 "2006-05-12T16:38:57Z")

</div>

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

def try\_it  
bobik = make\_class()  
argh = bobik.new  
p argh.class  
end

try\_it

==\> #Class:0x2c17010

This looks like a mismatch between two unrelated features (true,  
assigning to constants dynamically is fishy, but creating new classes  
isn’t !), right ?

And why the class name ‘Bobik’ is printed, while #Class:0x2c17010 is  
printed when the class name is in lowercase. Does the lowercase change  
anything semantically ?

---

<div class="post-metadata">

**Author:** ![eliben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3bc359/32.png) [@eliben](https://www.ruby-forum.com/u/eliben)\
**Post date:** [May 12, 2006, 5:14pm UTC](https://www.ruby-forum.com/t/an-inherent-mismatch-when-creating-dynamic-classes/60359/2 "2006-05-12T17:14:12Z")

</div>

2006/5/12, Eli B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> ```
> Joe = 5
> 
> ```
> 
> end  
> def make\_class
> 
> def make\_class  
> Class.new  
> end

This method is superfluous. You can simply do “bobik = Class.new” in  
this method:

> 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 #Class:0x2c17010 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  
> > =\> #Class:0x829f4b0  
> > 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

---

<div class="post-metadata">

**Author:** ![eliben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3bc359/32.png) [@eliben](https://www.ruby-forum.com/u/eliben)\
**Post date:** [May 12, 2006, 5:20pm UTC](https://www.ruby-forum.com/t/an-inherent-mismatch-when-creating-dynamic-classes/60359/3 "2006-05-12T17:20:37Z")

</div>

> > 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.

IMHO it’s a mismatch because when I want to create a class dynamically  
in some method, I just want to give it a Name like any other class,  
beginning with an uppercase letter. But I can’t, because it is  
considered a dynamic constant assignment. So I’m forced to name it  
against the Ruby convention (or use const\_set)

> > > c = Class.new  
> > > =\> #Class:0x829f4b0
> > 
> > > 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.

If that’s the only difference, it’s OK I guess.

---

<div class="post-metadata">

**Author:** ![eliben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3bc359/32.png) [@eliben](https://www.ruby-forum.com/u/eliben)\
**Post date:** [May 12, 2006, 7:29pm UTC](https://www.ruby-forum.com/t/an-inherent-mismatch-when-creating-dynamic-classes/60359/4 "2006-05-12T19:29:10Z")

</div>

On 5/12/06, Eli B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> considered a dynamic constant assignment. So I’m forced to name it
> 
> > Classes do not have a proper name until assigned a constance. But  
> > otherwise they are fully functional. No problem with that.
> 
> If that’s the only difference, it’s OK I guess.
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

If you really need to set the constant you can also try something like:

Object.const\_set(“NewClassName”, klass\_obj)

pth

---

<div class="post-metadata">

**Author:** ![eliben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3bc359/32.png) [@eliben](https://www.ruby-forum.com/u/eliben)\
**Post date:** [May 12, 2006, 7:51pm UTC](https://www.ruby-forum.com/t/an-inherent-mismatch-when-creating-dynamic-classes/60359/5 "2006-05-12T19:51:08Z")

</div>

On Sat, May 13, 2006 at 12:20:52AM +0900, Eli B. wrote:

> considered a dynamic constant assignment. So I’m forced to name it  
> against the Ruby convention (or use const\_set)

But dynamically created classes are not constant, so just assign them to  
a (lowercase initialled) variable and pass it around. It is _you_  
wanting  
to assign something dynamically created to a constant that’s the  
problem.  
Why does your dynamically created class need a constant name?

Commonly defined classes are using uppercase because those are  
really just constants holding an instance of Class, created by  
magic and syntactic sugar. So it turns out those features are not  
unrelated  
at all, and class naming just follows from the constant naming rule.

Juergen

---

<div class="post-metadata">

**Author:** ![eliben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3bc359/32.png) [@eliben](https://www.ruby-forum.com/u/eliben)\
**Post date:** [May 14, 2006, 12:28pm UTC](https://www.ruby-forum.com/t/an-inherent-mismatch-when-creating-dynamic-classes/60359/6 "2006-05-14T12:28:45Z")

</div>

2006/5/12, Juergen S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Why does your dynamically created class need a constant name?  
> Exactly! Thanks for voicing my concerns much better than I did.  
> Adding to that, what’s the use of a class name of a dynmic created  
> class? The name won’t be known to other parts of the code beforehand  
> so it cannot be used for referencing the class.

Kind regards

robert
