# Why does this include fail in instance\_exec?

**URL:** <https://rubytalk.org/t/why-does-this-include-fail-in-instance-exec/54156>\
**Category:** ruby-talk\
**Created:** [27 June 2009 06:01 UTC](https://rubytalk.org/t/why-does-this-include-fail-in-instance-exec/54156 "2009-06-27T06:01:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Charlton](https://avatars.discourse-cdn.com/v4/letter/c/c5a1d2/32.png) [@Charlton](https://rubytalk.org/u/Charlton)\
**Post date:** [27 June 2009 06:01 UTC](https://rubytalk.org/t/why-does-this-include-fail-in-instance-exec/54156/1 "2009-06-27T06:01:27Z")

</div>

The following code will dynamically create a class, C, inside of module  
A. Class C would like to include module B which is also defined in  
module A but the 'include B' line for C fails with `module\_eval':  
uninitialized constant Class::B (NameError).

I've included a class D definition as well which obviously works.

&nbsp;&nbsp;&nbsp;&nbsp;module A  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;def create\_class(c, &b)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;klass = A.const\_set(c.capitalize, Class.new)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;klass.instance\_exec(&b)  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;A.module\_eval \<\<-EOF  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;module B  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def self.append\_features(m)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "B"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class D  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;include B  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;create\_class :c do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;include B  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;EOF

Any insight would be greatly appreciated.

Thanks,  
Charlton

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Charlton](https://avatars.discourse-cdn.com/v4/letter/c/c5a1d2/32.png) [@Charlton](https://rubytalk.org/u/Charlton)\
**Post date:** [27 June 2009 06:32 UTC](https://rubytalk.org/t/why-does-this-include-fail-in-instance-exec/54156/2 "2009-06-27T06:32:04Z")

</div>

I found a post that address this. I guess the nature of  
instance\_exec/eval has changed with respect to bindings.

[http://coderrr.wordpress.com/2009/06/02/fixing-constant-lookup-in-dsls-in-ruby-1-9/](http://coderrr.wordpress.com/2009/06/02/fixing-constant-lookup-in-dsls-in-ruby-1-9/)

Charlton Wang wrote:

> **···**
>
> > The following code will dynamically create a class, C, inside of module  
> > A. Class C would like to include module B which is also defined in  
> > module A but the 'include B' line for C fails with `module\_eval':  
> > uninitialized constant Class::B (NameError).
> > 
> > I've included a class D definition as well which obviously works.
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;module A  
> > &nbsp;&nbsp;&nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;def create\_class(c, &b)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;klass = A.const\_set(c.capitalize, Class.new)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;klass.instance\_exec(&b)  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;A.module\_eval \<\<-EOF  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;module B  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def self.append\_features(m)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "B"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class D  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;include B  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;create\_class :c do  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;include B  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;EOF
> > 
> > Any insight would be greatly appreciated.
> > 
> > Thanks,  
> > Charlton
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
