Working with classes and modules

Hi all,

In my rails app I’ve written (with not so many chars) this:

if module_or_str_or_sym == controller.class.to_s.split('::')[0]

I had thought about this:

if eval(module_or_str_or_sym).const_defined? controller.class

but for obvious reasons abandoned it. (1. it doesn’t work
(controller.class is not a string). 2. eval seems like a bad idea.)

This got me wishing for a few things:

() A to_const method def for Sting and Symbol instead of eval.
) A way to traverse the nesting of a given class/module e.g.
controller.class.nesting # => [MyModule, MyController]
(*) A method to determine if a class/module is nested in another
class/module. For instance
controller.class.nesting.include? module_or_str_or_sym.to_const

I’d like to know if anyone else thinks this would be useful or already
has a solution.


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs