Xavier Noria
1/19/2008 12:30:00 AM
On Jan 19, 2008, at 1:07 AM, Xavier Noria wrote:
> On Jan 19, 2008, at 12:29 AM, Daniel Brumbaugh Keeney wrote:
>
>> Is there an obvious method for a module like
>> A::B::C
>> that would result in A::B?
>
> What do you mean?
Oh, perhaps you just want something like Module#parent provided by
Active Support. There the parent is computed by hand from the module
name:
def parent
parent_name = name.split('::')[0..-2] * '::'
parent_name.empty? ? Object : parent_name.constantize
end
where String#constantize is
def constantize(camel_cased_word)
unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ camel_cased_word
raise NameError, "#{camel_cased_word.inspect} is not a valid
constant name!"
end
Object.module_eval("::#{$1}", __FILE__, __LINE__)
end
-- fxn