From: dblack@... Date: 2006-01-09T22:04:06+09:00 Subject: Re: Ideas on "Why Living Dangerous can be A Good Thing" in Ruby? Hi -- On Mon, 9 Jan 2006, Steve Litt wrote: > On Sunday 08 January 2006 06:14 pm, dblack@wobblini.net wrote: >> >> I'm not sure what you mean by adding an instance variable to a class, >> but as for methods, they're all defined at runtime. >> >> class C >> def m >> end >> end >> >> That code gets executed, and then there's a class called C with an >> instance method m. >> >> I'm not sure what it is that you consider subclassing to be a better >> choice than. Are you talking about define_method? Or the practice of >> reopening core classes? >> >> >> David > > Hi David, > > I don't know for sure, because when I read about this stuff my first reaction > was "I'm not gonna do that!" > > I got the impression I could do this: > > myclass = MyClass.new > myclass.never_seen_before_attribute = 5 > > As I said, I didn't research it because I considered its use to be a negative, > but stored it in the back of my mind in case I had to maintain code like > that. I think you mean method_missing. It has nothing to do with instance variables; it's just a hook or callback that you can define to intercept calls to non-existent methods. What you do at that point is up to you. You can also just not define it, in which case a call to a non-existent message will raise an exception. There's nothing inherently dangerous or non-deterministic about method_missing. It's basically a control-flow technique. David -- David A. Black dblack@wobblini.net "Ruby for Rails", from Manning Publications, coming April 2006! http://www.manning.com/books/black