user1463822
user1463822

Reputation: 887

Ruby: module A vs class A::B

So, I can have something like this (A):

module A
    class B
    end
end

and also I can have this (B):

class A::B
end

Why would I use A instead of B and vice versa?

Upvotes: 2

Views: 236

Answers (2)

sawa
sawa

Reputation: 168081

When you are defining A::B for the first time, you have to take the first option. Otherwise, The second option saves indentation and is more compact, which is useful when you want to write something to A::B concisely and independent of other things in A.

Upvotes: 1

Michał Młoźniak
Michał Młoźniak

Reputation: 5556

Because in option B module A must be already defined. There are also some other issues with this option. You can read more about it here http://techblog.thescore.com/how-you-nest-modules-matters-in-ruby/

Upvotes: 1

Related Questions