module/class question

Are these two below the same thing?

1. module X
       module Y
         class Z

2. class X::Y::Z

Providing class Z has already been created with style 1, yes.

If you try to run style 2 without X and Y having been created you will get “NameError: uninitialized constant X”.

The difference between class and module in Ruby is fairly minimal (and mainly used for inferring a difference in usage to the reader)

Cheers,

Andy

ok as far as controllers are concerned in a RoR app would either of those style matter as far as name spacing?

Nope, won’t matter a jot.

But if you have this:

class Admin::FooController

end

without declaring Admin first you’ll get an error.

What I tend to do is this:

module Admin

class BaseController

def some_admin_method

end

end

end

Because you need to declare Admin before you can just randomly start using it. Then I do this when declaring a subclass:

class PagesController < Admin::BaseController

end

Cheers,

Andy

OK, that is what I thought, thanks.