From: James Gray Date: 2009-08-07T03:53:22+09:00 Subject: Re: Kernel's module methods? On Aug 6, 2009, at 11:32 AM, Joel VanderWerf wrote: > James Gray wrote: >> Yes, and I generally prefer extend(self) to module_function after >> much time trying it both ways. Unfortunately, I always seem to >> eventually run into issues after using module_function, say with >> constant resolution or included modules (like you mentioned). > > Can you post an example of the constant resolution problem next time > you come across it? I'm curious because I'm in the habit of using > module_function rather than extend(self), not for any good reason > except that it is perhaps more self documenting. Here's an example I remember from just the other day (though it probably falls more under the included module problem). Consider this simple module: module MoreMath extend Math # needed for self::sqrt include Math # needed for self#sqrt and PI module_function def circ(r) 2 * PI * r end def dist(x1, y1, x2, y2) sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2) end end p MoreMath.circ(12) p MoreMath.dist(0, 0, 3, 4) Note that I need both an extend and include to get just that much to work. Or, I can just do: module MoreMath include Math extend self def circ(r) 2 * PI * r end def dist(x1, y1, x2, y2) sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2) end end p MoreMath.circ(12) p MoreMath.dist(0, 0, 3, 4) Anyway, I too like module_function() and have used it a lot. I like how it makes the instance methods private so they don't add to an object's public interface when mixed in. It just seems like I have more control with extend(self) so I tend to reach for that now. James Edward Gray II