Name that method!

I

Intransition

What would you call a method that did this:

Foo::Bar::Baz.some_method #=> Foo::Bar
 
A

Andrew Wagner

[Note: parts of this message were removed to make it a legal post.]

I'll vote for parent_namespace.
 
B

Benoit Daloze

I'll vote for parent_namespace.

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)
 
I

Intransition

I prefer #enclosing_module since the "namespace" is a Module.

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

Facets has a Kernel method #constant:

def constant(const)
const =3D const.to_s.dup
base =3D 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.
 
R

Robert Dober

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.


--=20
The best way to predict the future is to invent it.
-- Alan Kay
 
I

Intransition

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.

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 ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,145
Messages
2,570,828
Members
47,374
Latest member
anuragag27

Latest Threads

Top