When to use nested classes and classes nested in modules?

后端 未结 5 640
无人及你
无人及你 2020-12-12 10:26

I\'m pretty familiar with when to use subclasses and modules, but more recently I\'ve been seeing nested classes like this:

class Foo
  class Bar
    # do so         


        
5条回答
  •  余生分开走
    2020-12-12 10:53

    You probably want to use this to group your classes into a module. Sort of a namespace thing.

    for example the Twitter gem uses namespaces to achieve this:

    Twitter::Client.new
    
    Twitter::Search.new
    

    So both Client and Search classes live under the Twitter module.

    If you want to check the sources, the code for both classes can be found here and here.

    Hope this helps!

提交回复
热议问题