How do you find the namespace/module name programmatically in Ruby on Rails? -
how find name of namespace or module 'foo' in filter below?
class applicationcontroller < actioncontroller::base def get_module_name @module_name = ??? end end class foo::barcontroller < applicationcontroller before_filter :get_module_name end
none of these solutions consider constant multiple parent modules. instance:
a::b::c
as of rails 3.2.x can simply:
"a::b::c".deconstantize #=> "a::b"
as of rails 3.1.x can:
constant_name = "a::b::c" constant_name.gsub( "::#{constant_name.demodulize}", '' )
this because #demodulize opposite of #deconstantize:
"a::b::c".demodulize #=> "c"
if need manually, try this:
constant_name = "a::b::c" constant_name.split( '::' )[0,constant_name.split( '::' ).length-1]
Comments
Post a Comment