From: Pat Maddox Date: 2006-05-16T06:26:10+09:00 Subject: Re: begining programmer questions On 5/15/06, Jake McArthur wrote: > On May 15, 2006, at 12:50 PM, Regg wrote: > > > I would have hoped that once a type has been established for an > > object, > > the object would only adhere to the rules of that object...not change > > itself into a new object type. > > This is the case. However, variables are only references and are > themselves "typeless," in a sense. While you cannot change the object > a variable points to from one type to another, you can have the > variable point to a different object entirely which can be of a > different type. Perhaps this is nitpicking, perhaps not, but it's incorrect to say that you cannot change an object's type (to paraphrase). class Dog def woof; "woof"; end end class Duck def quack; "quack"; end end dog = Dog.new Okay so dog points to a Dog object, and is obviously not a Duck. Can we change its type to Duck though? Instead of full on changing it, can we ADD the Duck type? Yeah, it's pretty simple: def dog.quack "identity crisis" end You can now call dog.quack, and Duck is one of its types. You might say "Well no, all you did is add a quack method to that particular object, it's still a Dog." You're right...but for the most part we don't care about an object's class - that's only interesting at creation time. An object's type is simply its interface, and as long as my Dog object responds to each method that a Duck does, then the Dog is also of type Duck. That may seem kind of confusing, and that's really because you shouldn't be thinking of it in these partitioned terms anyway. All we really care about when playing with an object is whether we can make method calls on it without our program blowing up. Pat