non-constant class names

C

Chris Pine

Out of curiosity, why are we prevented from making non-constant class
names? We can make anonymous classes, we can assign them to
variables, so why not this?

class foo
end

Any good reason?

Chris
 
A

Assaph Mehr

Chris said:
Out of curiosity, why are we prevented from making non-constant class
names? We can make anonymous classes, we can assign them to
variables, so why not this?

class foo
end

Any good reason?

I think it's a design issue - Ruby (well Matz :) is trying to make you
program coherently.

Yes, you can do:
foo = Class.new do ... end
But you should think about what you're trying to achieve. Seeing
something like this in the code should alert you that there is some
clever, subtle trick going on.

When you look at the Code you should be able to understand it easily by
reading it. Keeping is simple - some_text is a method, ALL_COLORS is a
constant and SomeThing is a class - makes it easier. In your example is
'foo' a variable in some scope? a method from some parent class /
included module?

BTW, there are classes with small letters only:
irb(main):001:0> ObjectSpace.each_object(Class) { |k| p k if k.name =~
/^[a-z]/ }

#=> fatal

But that is obviously a very special case.

Cheers,
Assaph
 
S

Saynatkari

Le 19/4/2005 said:
Out of curiosity, why are we prevented from making non-constant class
names? We can make anonymous classes, we can assign them to
variables, so why not this?

class foo
end

class Foo
def foo; puts 'foo'; end;
end

foo = Foo
foo.new.foo

Any good reason?

Some conceptual reason, I am sure :)

E
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top