# Access module's anonymous parent

**URL:** <https://www.ruby-forum.com/t/access-modules-anonymous-parent/151867>\
**Category:** Ruby\
**Created:** [October 27, 2008, 9:15am UTC](https://www.ruby-forum.com/t/access-modules-anonymous-parent/151867 "2008-10-27T09:15:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![xavier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/2bfe46/32.png) [@xavier](https://www.ruby-forum.com/u/xavier)\
**Post date:** [October 27, 2008, 9:15am UTC](https://www.ruby-forum.com/t/access-modules-anonymous-parent/151867/1 "2008-10-27T09:15:40Z")

</div>

I want the following eval to output “Foo called”, without having a named  
constant, since in leaks in to the calling scope:

m = Module.new  
m.instance\_eval do  
def foo  
puts “Foo called”  
end  
ParentSelf = self  
class self::A  
ParentSelf.foo # How do I do this without a constant?  
end  
end  
a = m.const\_get(‘A’).new  
puts ParentSelf # This should raise - constant should not exist

I have found given self and a global function, I can use the string  
representation of the anon module to find it in ObjectSpace. Clearly  
this is dodgy though:

def depend(instance, file)  
q = self.to\_s.split(’::’).first.to\_s  
found = nil  
ObjectSpace.each\_object(Module) do |m|  
found = m if q == m.to\_s  
end  
found.depend(file)  
end

m = Module.new  
m.instance\_eval do  
def depend(file)  
puts “Depend called with file: #{file}”  
end

class self::A  
depend(self, ‘afile.rb’)  
end  
end

---

<div class="post-metadata">

**Author:** ![xavier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/2bfe46/32.png) [@xavier](https://www.ruby-forum.com/u/xavier)\
**Post date:** [October 27, 2008, 9:41am UTC](https://www.ruby-forum.com/t/access-modules-anonymous-parent/151867/2 "2008-10-27T09:41:22Z")

</div>

Is this what you’re looking for?

m = Module.new  
m.instance\_eval do  
def foo  
puts “Foo called”  
end  
parent = self  
child = class self::A; self; end  
child.class\_eval { parent.foo }  
end  
a = m.const\_get(‘A’).new  
puts ParentSelf # This should raise - constant should not exist

(you can also use ‘m.foo’ instead of ‘parent.foo’, which means you can  
remove the assignment ‘parent = self’)

---

<div class="post-metadata">

**Author:** ![xavier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/2bfe46/32.png) [@xavier](https://www.ruby-forum.com/u/xavier)\
**Post date:** [October 27, 2008, 10:54pm UTC](https://www.ruby-forum.com/t/access-modules-anonymous-parent/151867/3 "2008-10-27T22:54:53Z")

</div>

Brian C. wrote:

> Is this what you’re looking for?
> 
> m = Module.new  
> m.instance\_eval do  
> def foo  
> puts “Foo called”  
> end  
> parent = self  
> child = class self::A; self; end  
> child.class\_eval { parent.foo }  
> end  
> a = m.const\_get(‘A’).new  
> puts ParentSelf # This should raise - constant should not exist
> 
> (you can also use ‘m.foo’ instead of ‘parent.foo’, which means you can  
> remove the assignment ‘parent = self’)

I would have liked to keep the more familiar class syntax without  
resorting to class\_eval

> parent = self  
> child = class self::A; self; end  
> child.class\_eval { parent.foo }

I’m autoloading a file, it contains this part, so it’s basically the  
public interface.

> class self::A  
> parent.foo

And for that reason this is a much nicer, I think. But if it can’t be  
done like this, it can’t be done…

---

<div class="post-metadata">

**Author:** ![xavier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/2bfe46/32.png) [@xavier](https://www.ruby-forum.com/u/xavier)\
**Post date:** [October 28, 2008, 8:06am UTC](https://www.ruby-forum.com/t/access-modules-anonymous-parent/151867/4 "2008-10-28T08:06:03Z")

</div>

2008/10/27 Xavier S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I’m autoloading a file, it contains this part, so it’s basically the  
> public interface.
> 
> > class self::A  
> > parent.foo
> 
> And for that reason this is a much nicer, I think. But if it can’t be  
> done like this, it can’t be done…

Xavier, I don’t know why you want to have an anonymous module with a  
local constant, but if you really need this and are willing to change  
the public interface a little bit you could do

m = Module.new  
m.module\_eval do  
# methods for dependent classes  
def depend(file)  
puts “#{self} depends on #{file}”  
end  
# methods to define a dependent class  
def self.const\_missing(name)  
const\_set(name, Class.new)  
end  
def self.dependent\_class(cls, &blk)  
parent\_module = self  
cls.class\_eval do extend(parent\_module) end  
cls.class\_eval(&blk)  
end  
# public interface for dependent classes  
dependent\_class self::A do  
depend(“afile.rb”)  
end  
end

Regards,  
Pit

---

<div class="post-metadata">

**Author:** ![xavier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/2bfe46/32.png) [@xavier](https://www.ruby-forum.com/u/xavier)\
**Post date:** [October 28, 2008, 8:43am UTC](https://www.ruby-forum.com/t/access-modules-anonymous-parent/151867/5 "2008-10-28T08:43:57Z")

</div>

Pit C. wrote:

> 2008/10/27 Xavier S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > I’m autoloading a file, it contains this part, so it’s basically the  
> > public interface.
> > 
> > > class self::A  
> > > parent.foo
> > 
> > And for that reason this is a much nicer, I think. But if it can’t be  
> > done like this, it can’t be done…
> 
> Xavier, I don’t know why you want to have an anonymous module with a  
> local constant  
> Hot loading of code. I’m being a bit anal about it, also I’m just  
> curious 🙂  
> Would be nice to not need the dependent\_class bit, but it’s not so bad.

I had to hack your code a bit to get what I want, which is this:

m = Module.new  
m.instance\_eval do  
self::Depend = []

def self.const\_missing(name)  
const\_set(name, Class.new)  
end

def self.dependent\_class(cls, &blk)  
parent\_module = self  
(class \<\< cls; self; end).send(:define\_method, :depend) do |file|  
parent\_module::Depend \<\< file  
end  
cls.class\_eval(&blk)  
end

dependent\_class self::A do  
depend(“afile.rb”)

```
def foo
  "foo"
end

```

end  
end

a = m.const\_get(‘A’).new  
puts a.foo # =\> foo  
puts m::Depend.inspect # =\> [“afile.rb”]

Thanks!  
Xavier
