Ruby (and Rails) nested module syntax

前端 未结 4 1858
梦如初夏
梦如初夏 2020-12-29 03:09

I\'m wondering what the difference is between the following two modules

# First Example
module Parent
  module Child
  end
end

and

4条回答
  •  一向
    一向 (楼主)
    2020-12-29 04:06

    I prefer the second method (if im sure that Parent is already defined) because it looks cleaner, esp. when the nesting is very deep.

    However the 1st method has some advantages, one not yet discussed is that a nested module gets access to any lexically available constants in the enclosing module.

提交回复
热议问题