On Sunday 08 January 2006 06:21 pm, 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.  It doesn't make sense to add *all* the possible
> combinations as methods to start with, when you can just create them
> on demand.
> Only a small subset of all the possible variations will ever by used
> by a
> particular application anyway.  Using subclasses in this case doesn't
> help. How
> would you decide which methods to put in the subclass?   You would have
> to lock in some choices and that would prevent you from, for example,
> having
> a fully dynamic query interface to your application.
>
>
> Gary Wright

Hi Gary,

I'm trying to understand this. If Model.find_by_city was never defined, how 
can it know what to do? I think I'm missing some big chunk of Ruby knowledge 
here.

Are you saying that when Model.find_by_city is executed, the model looks at 
the database, discovers there's a city column, and on the fly constructs a 
method to search the table by city?

SteveT

Steve Litt
http://www.troubleshooters.com
slitt / troubleshooters.com