# Class in instance\_eval -- what the namespace?

**URL:** <https://rubytalk.org/t/class-in-instance-eval-what-the-namespace/58367>\
**Category:** ruby-talk\
**Created:** [1 May 2010 19:20 UTC](https://rubytalk.org/t/class-in-instance-eval-what-the-namespace/58367 "2010-05-01T19:20:29Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Simon\_Chiang](https://avatars.discourse-cdn.com/v4/letter/s/7feea3/32.png) [@Simon\_Chiang](https://rubytalk.org/u/Simon_Chiang)\
**Post date:** [1 May 2010 19:20 UTC](https://rubytalk.org/t/class-in-instance-eval-what-the-namespace/58367/1 "2010-05-01T19:20:29Z")

</div>

I'm confused as to how the parent constant is determined when you  
define a class within an instance\_eval. It appears that sometimes  
ruby decides to use a nameless, perhaps temporary class as the parent  
constant.

Interestingly enough, the behavior is different when you use a block  
vs a string, and ruby implementation to implementation. For example  
consider this script:

&nbsp;&nbsp;[script.rb]  
&nbsp;&nbsp;obj = Object.new

&nbsp;&nbsp;obj.instance\_eval do  
&nbsp;&nbsp;&nbsp;&nbsp;class A; end  
&nbsp;&nbsp;&nbsp;&nbsp;puts A  
&nbsp;&nbsp;end  
&nbsp;&nbsp;puts A rescue puts $!.message

&nbsp;&nbsp;obj.instance\_eval %q{  
&nbsp;&nbsp;&nbsp;&nbsp;class B; end  
&nbsp;&nbsp;&nbsp;&nbsp;puts B  
&nbsp;&nbsp;}  
&nbsp;&nbsp;puts B rescue puts $!.message

Running this across several implementations produces a variety of  
results:

&nbsp;&nbsp;% rvm ruby script.rb  
&nbsp;&nbsp;jruby-1.4.0: jruby 1.4.0 (ruby 1.8.7 patchlevel 174) (2009-11-02  
69fbfa3) (Java HotSpot(TM) 64-Bit Server VM 1.6.0\_17) [x86\_64-java]  
&nbsp;&nbsp;A  
&nbsp;&nbsp;A  
&nbsp;&nbsp;#\<Class:01x3a98350a\>::B  
&nbsp;&nbsp;uninitialized constant B  
&nbsp;&nbsp;rbx-1.0.0-rc4: rubinius 1.0.0-rc4 (1.8.7 release 2010-03-31 JI)  
[x86\_64-apple-darwin10.3.0]  
&nbsp;&nbsp;A  
&nbsp;&nbsp;A  
&nbsp;&nbsp;B  
&nbsp;&nbsp;B  
&nbsp;&nbsp;ruby-1.8.7-p249: ruby 1.8.7 (2010-01-10 patchlevel 249) [i686-  
darwin10.2.0]  
&nbsp;&nbsp;A  
&nbsp;&nbsp;A  
&nbsp;&nbsp;#\<Class:0x10019c3b8\>::B  
&nbsp;&nbsp;uninitialized constant B  
&nbsp;&nbsp;ruby-1.9.1-p378: ruby 1.9.1p378 (2010-01-10 revision 26273) [i386-  
darwin10.2.0]  
&nbsp;&nbsp;#\<Class:0x000001008481e0\>::A  
&nbsp;&nbsp;uninitialized constant A  
&nbsp;&nbsp;#\<Class:0x000001008481e0\>::B  
&nbsp;&nbsp;uninitialized constant B

Any thoughts? And specifically, does anyone know of a way to set the  
default namespace for the class keyword? Specifying the full constant  
name like 'class ::A' works to assign the constants and uniformly  
produce a rubinius-like output but I was wondering if there is another  
way.
