简体   繁体   中英

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?

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/

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM