From: James Britt Date: 2006-01-09T09:15:11+09:00 Subject: Re: Ideas on "Why Living Dangerous can be A Good Thing" in Ruby? gwtmp01@mac.com wrote: > > On Jan 8, 2006, at 5:47 PM, Steve Litt wrote: > >> Personally, I'd NEVER gratuitously add a method or instance var to a >> class at >> runtime. If I needed more methods than the class provided, I'd >> subclass it. > > > I'm not sure I follow this. You think that subclassing at runtime is > better > in some way than simply adding a method to a class? It is hard to talk > about > this stuff in the abstract. For example, I feel differently about > dynamically > adding methods to Array than I would to a class I created to solve a > particular > problem. > > I think it does take a certain amount of paradigm shifting to see > solutions to > problems that involve dynamically creating methods in classes or on > particular > objects. Until you make that shift those facilities seem quite abstract. > > For example, in Rails you can call methods like: > > Model.find_by_date > Model.find_by_name > Model.find_by_city > Model.find_by_city_and_date > > Those methods don't exist until you call them at which point they are > defined > and added to the class. Really? I thought they were simply reparsed by method_missing into messages of the form find( ) asinvoked, not turned in "permanent" methods. Interesting. Which raises a question: Is it metaprogramming if I use method_missing to parse messages, see if it can reformulate the message into a known, workable form, and then redispatch by invoking 'send'? No code writing, no method adding. Is that not simply the point of method_missing; i.e., it is simply a Ruby/OO idiom. Messages need not map to methods. And then isn't adding them as methods just an optimization technique to avoid the cost of calling method_missing on repeated invocations? It's memoization for behavior. Is memoization metaprogramming, or is it only metaprogramming when behavior is altered? James -- http://www.ruby-doc.org - Ruby Help & Documentation http://www.artima.com/rubycs/ - Ruby Code & Style: Writers wanted http://www.rubystuff.com - The Ruby Store for Ruby Stuff http://www.jamesbritt.com - Playing with Better Toys http://www.30secondrule.com - Building Better Tools