From: "David A. Black" Date: 2005-02-01T04:42:04+09:00 Subject: Re: Should every method have a module method? Hi -- On Tue, 1 Feb 2005, Trans wrote: > I'm looking at some the Rail's support methods to see what Facets can > gain from it. The following gives me pause (slightly simplified for > your inspection): > > | module Inflector > | extend self > | > | def pluralize(word) > | result = word.to_s.dup > | plural_rules.each do |(rule, replacement)| > | break if result.gsub!(rule, replacement) > | end > | return result > | end > | ... > | > | > | class String > | module Inflections > | def pluralize > | Inflector.pluralize(self) > | end > | ... > | > | class String > | include Inflections > | end (Small request: can you not put | at the beginning of code lines? It makes them hard to cut-and-paste. Just indenting is fine.) > Is all this really neccessary? Are these methods really ever usful for > anything else but Strings and derivatives thereof? --What would you > include them in? And would you ever really be more inclined to write: > > Inflector.pluralize(a) > > over > > a.pluralize > > Perhaps some people use Ruby in completely non-OOP ways. I guess they > would appriciate this design. If so should that be something we should > always try to accomodate? It's not strictly a OOP/non-OOP thing; it's a clash thing. It's the same old "prisoner's dilemma"-esque problem as always: if we all agree on what core changes can be made dynamically, we can make them, but if we don't, we can't safely make any. This was one of the first things I worked on when I got interested in Ruby, and the result was the almost-proof-of-concept project Ruby Behaviors, which offered a mechanism for block-scoped changes to the core language. It wasn't thread-safe, though. There are some other similar packages that do similar things, one of which I believe is thread-safe. And Matz has been talking about some kind of namespace facility in Ruby 2.0 to allow for this kind of thing too. David -- David A. Black dblack@wobblini.net