From: dblack@... Date: 2002-09-12T19:13:03+09:00 Subject: Re: New newbies Qs on Ruby Hello -- On Thu, 12 Sep 2002, Overnight wrote: > Hello! > > As I explore Ruby, I like it more and more, but of course more and more > questions arise, as well. > > 1) It is not clear to me what is the right idiom to use when a module is > used only as a collection of some main (i.e. published) functions, > implemented by other internal (i.e. private) functions... i.e. the Module > is not intended to be included anywhere (module Math could be an > example of what I mean). > > Suppose that module M publishes an 'm' function, which is implemented by > 'm1', which in turn uses 'm2'. > > Having exluded the 'module_function' approach: > since it won't work: > > M.m -> mod3.rb:11:in `m': undefined local variable or method `m1' for > M:Module (NameError) > > > I tried: [object.extend approach] > > and: > > > module M > private > def M.m2 > puts "In m2" > end > > def M.m1 > m2 > end > > public > def M.m > m1 > end > end > > M.m -> In m2 > > > Both work, but I feel like none is the right one... The first solution > requires creating and than extending an object, just for the purpose of > having something to which "attach" M's instance methods, and it looks > ugly. The second solution looks more "clean" and maybe I'm just not OO > enough to accept it :-) but IMHO m1 and m2 would "look better" as > instance methods. > > So... is there another "right" way to get what I want? Actually m1 and m2 *are* instance methods; the instance of which they are methods just happens to be an instance of class Module :-) If you want to avoid the explicity M. syntax, you can temporarily enter into the virtual class of M, where (as in all class definitions) the methods you create are instance methods of instances of the class (M being the only instance of this particular class). module M class << self private def m2 puts "In m2" end def m1 m2 end public def m m1 end end end David -- David Alan Black | Register for RubyConf 2002! home: dblack@candle.superlink.net | November 1-3 work: blackdav@shu.edu | Seattle, WA, USA Web: http://pirate.shu.edu/~blackdav | http://www.rubyconf.com