# Name that method!

**URL:** <https://www.ruby-forum.com/t/name-that-method/194003>\
**Category:** Ruby\
**Created:** [August 27, 2010, 6:02pm UTC](https://www.ruby-forum.com/t/name-that-method/194003 "2010-08-27T18:02:54Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [August 27, 2010, 6:02pm UTC](https://www.ruby-forum.com/t/name-that-method/194003/1 "2010-08-27T18:02:54Z")

</div>

What would you call a method that did this:

Foo::Bar::Baz.some\_method #=\> Foo::Bar

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [August 27, 2010, 6:18pm UTC](https://www.ruby-forum.com/t/name-that-method/194003/2 "2010-08-27T18:18:08Z")

</div>

I’ll vote for parent\_namespace.

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [August 27, 2010, 11:38pm UTC](https://www.ruby-forum.com/t/name-that-method/194003/3 "2010-08-27T23:38:55Z")

</div>

Module#enclosing\_module ??

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [August 28, 2010, 10:25am UTC](https://www.ruby-forum.com/t/name-that-method/194003/4 "2010-08-28T10:25:42Z")

</div>

On Fri, Aug 27, 2010 at 11:38 PM, Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Module#enclosing\_module ??  
> (+1)

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [August 28, 2010, 4:57pm UTC](https://www.ruby-forum.com/t/name-that-method/194003/5 "2010-08-28T16:57:16Z")

</div>

On 27 August 2010 18:11, Andrew W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’ll vote for parent\_namespace.

On 27 August 2010 23:38, Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Module#enclosing\_module ??

I prefer #enclosing\_module since the “namespace” is a Module.

But how would you implement that? (Except using the name of course)

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [August 28, 2010, 5:06pm UTC](https://www.ruby-forum.com/t/name-that-method/194003/6 "2010-08-28T17:06:03Z")

</div>

On Aug 28, 10:56 am, Benoit D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> But how would you implement that? (Except using the name of course)  
> Facets has a Kernel method #constant:

def constant(const)  
const = const.to\_s.dup  
base = const.sub!(/^::/, ‘’) ? Object : ( self.kind\_of?(Module) ?  
self : self.class )  
const.split(/::/).inject(base){ |mod, name| mod.const\_get(name) }  
end

So something like:

class Module  
def enclosing\_module  
constant(name.split(‘::’)[0…-1].join(‘::’)  
end  
end

I am all ears for improvements to the implementation.

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [August 28, 2010, 5:00pm UTC](https://www.ruby-forum.com/t/name-that-method/194003/7 "2010-08-28T17:00:12Z")

</div>

On Aug 27, 5:38 pm, Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Module#enclosing\_module ??

Foo::Bar could be class. Does that make a difference?

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [August 28, 2010, 10:33pm UTC](https://www.ruby-forum.com/t/name-that-method/194003/8 "2010-08-28T22:33:17Z")

</div>

On Aug 28, 11:10 am, Robert D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> However if some do not see a class as a (kind of a) module the name  
> would be chosen badly indeed.

Your right it is, and that should suffice, though I recall matz once  
talking about the possibility of Module not being a subclass of Class  
in Ruby 2.

Just the same, perhaps we can simplify it to just #enclosure ?

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [August 28, 2010, 5:10pm UTC](https://www.ruby-forum.com/t/name-that-method/194003/9 "2010-08-28T17:10:44Z")

</div>

On Sat, Aug 28, 2010 at 4:59 PM, Intransition [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> On Aug 27, 5:38 pm, Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Module#enclosing\_module ??
> 
> Foo::Bar could be class. Does that make a difference?

But a class is a module, right? Although more exactly one could argue  
that an instance of a class is a module. Well for me that was good  
enough an approximation.  
As a side note I never use class\_eval, but always module\_eval.  
However if some do not see a class as a (kind of a) module the name  
would be chosen badly indeed.

Cheers  
R.
