# "classes are always open" for anonymous classes?

**URL:** <https://www.ruby-forum.com/t/classes-are-always-open-for-anonymous-classes/59426>\
**Category:** Ruby\
**Created:** [May 3, 2006, 7:05pm UTC](https://www.ruby-forum.com/t/classes-are-always-open-for-anonymous-classes/59426 "2006-05-03T19:05:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![joshmh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/5fc32e/32.png) [@joshmh](https://www.ruby-forum.com/u/joshmh)\
**Post date:** [May 3, 2006, 7:05pm UTC](https://www.ruby-forum.com/t/classes-are-always-open-for-anonymous-classes/59426/1 "2006-05-03T19:05:36Z")

</div>

So you know how classes are always open? What about anonymous classes?

I try:

x = Class.new

class x  
def foo  
end  
end

but get:

SyntaxError: compile error  
(irb):2: class/module name must be CONSTANT  
from (irb):5  
from :0

I suppose class\_eval is the equivalent?

x.class\_eval {  
def foo  
end  
}

my\_x = x.new  
my\_x.foo

Unfortunately, that’s not as clear. I think that’s something I’m going  
to have to live with, but any pointers for things I’m missing?

Also, is there any reason that the “class” declaration couldn’t accept  
my first attempt at this (“class x; def foo; end; end”)?

Thanks,  
Josh

---

<div class="post-metadata">

**Author:** ![joshmh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/5fc32e/32.png) [@joshmh](https://www.ruby-forum.com/u/joshmh)\
**Post date:** [May 3, 2006, 7:05pm UTC](https://www.ruby-forum.com/t/classes-are-always-open-for-anonymous-classes/59426/2 "2006-05-03T19:05:38Z")

</div>

On Wed, 3 May 2006, Joshua H. wrote:

> So you know how classes are always open? What about anonymous classes?
> 
> I try:
> 
> x = Class.new
> 
> class x  
> def foo  
> end  
> end

try

class \<\< x  
def foo() 42 end  
end

or

x = Class.new{  
def foo() 42 end  
}

regards.

-a

---

<div class="post-metadata">

**Author:** ![joshmh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/5fc32e/32.png) [@joshmh](https://www.ruby-forum.com/u/joshmh)\
**Post date:** [May 3, 2006, 7:05pm UTC](https://www.ruby-forum.com/t/classes-are-always-open-for-anonymous-classes/59426/3 "2006-05-03T19:05:41Z")

</div>

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

> try
> 
> class \<\< x  
> def foo() 42 end  
> end

This one isn’t what he was looking for. The would be similar to doing:

class PhantomX  
def self.foo; 42 end  
end

Brian.

---

<div class="post-metadata">

**Author:** ![joshmh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/5fc32e/32.png) [@joshmh](https://www.ruby-forum.com/u/joshmh)\
**Post date:** [May 3, 2006, 7:05pm UTC](https://www.ruby-forum.com/t/classes-are-always-open-for-anonymous-classes/59426/4 "2006-05-03T19:05:42Z")

</div>

On 5/2/06, Joshua H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> SyntaxError: compile error  
> (irb):2: class/module name must be CONSTANT  
> from (irb):5  
> from :0

Yep. The irb error message is actually giving a pretty good hint here.  
Classes defined with the class keyword are always constants because they  
start with an uppercase letter.

I suppose class\_eval is the equivalent?

> x.class\_eval {  
> def foo  
> end  
> }
> 
> my\_x = x.new  
> my\_x.foo

Right again. You can also use that or you can assign to a constant:

X = x

class X  
def foo  
puts ‘bar’  
end  
end

y = x.new  
y.bar

Although, in that case, you might as well assign to the constant in the  
first place; although, if you continue down that road, there’s really no  
point in declaring it anonymously.
