What would you call a method that did this:
Foo::Bar::Baz.some_method #=> Foo::Bar
What would you call a method that did this:
Foo::Bar::Baz.some_method #=> Foo::Bar
I’ll vote for parent_namespace.
Module#enclosing_module ??
On 27 August 2010 18:11, Andrew W. [email protected] wrote:
I’ll vote for parent_namespace.
On 27 August 2010 23:38, Brian C. [email protected] 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)
On Aug 28, 10:56 am, Benoit D. [email protected] 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.
On Aug 27, 5:38 pm, Brian C. [email protected] wrote:
Module#enclosing_module ??
Foo::Bar could be class. Does that make a difference?
On Aug 28, 11:10 am, Robert D. [email protected] 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 ?
On Sat, Aug 28, 2010 at 4:59 PM, Intransition [email protected]
wrote:
On Aug 27, 5:38 pm, Brian C. [email protected] 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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs