From: "Aleksi Niemelä" Date: 2001-02-05T23:29:31+09:00 Subject: [ruby-talk:10373] Re: Nested modules and classes? On Sun, 4 Feb 2001, Guy N. Hurst wrote: > "Hal E. Fulton" wrote: > > ... > > Also, I don't really remember seeing any discussion > > here of nested classes... though apparently they > > exist. Are they ever Really Useful or just a mild > > curiosity? > > > > I have not yet seen an answer to this question, and > I am also interested in understanding this. > > I find nested modules and nested classes, > as well as nested classes inside nested modules. > > Nowhere have I seen this documented, yet I see it > used. What is the scoop? Nested modules are definitely useful as nested namespaces. Nested classes are not that useful in my experience. In Java you would use them for a substitute for blocks (procs) or (not so) anonymous routines. In Ruby they could be used in a same way, but as we have blocks already there's hardly any need for them used in that way. There's good use for nested classes. If you need a class carrying some data and methods, but feel that it's not important enough for the others to use or know about it, it might be beneficial to "localize" the definition, thus place it inside and near the code which uses it. - Aleksi module MyPackage module InnerPackage class TheWorker def initialize(foo) @foo = WorkersHelper.new(foo) end def foo @foo.calculate end class WorkersHelper def initialize(value) @value = value end def calculate @value ** 2 end end end end end puts MyPackage::InnerPackage::TheWorker.new(10).foo