Forum: Ruby An inherent mismatch when creating dynamic classes

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Eli B. (Guest)
on 2006-05-12 18:38
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 ?
Robert K. (Guest)
on 2006-05-12 19:14
(Received via mailing list)
2006/5/12, Eli B. <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
Eli B. (Guest)
on 2006-05-12 19:20
>> 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.
Patrick H. (Guest)
on 2006-05-12 21:29
(Received via mailing list)
On 5/12/06, Eli B. <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/.
>
>

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

Object.const_set("NewClassName", klass_obj)

pth
Juergen S. (Guest)
on 2006-05-12 21:51
(Received via mailing list)
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
Robert K. (Guest)
on 2006-05-14 14:28
(Received via mailing list)
2006/5/12, Juergen S. <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
This topic is locked and can not be replied to.