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

Popular posts from this blog

windows - Why does Vista not allow creation of shortcuts to "Programs" on a NonAdmin account? Not supposed to install apps from NonAdmin account? -

c++ - How do I get a multi line tooltip in MFC -

unit testing - How to mock PreferenceManager in Android? -