From: Luigi Ballabio Date: 2001-06-27T00:38:52+09:00 Subject: [ruby-talk:16907] Re: Something strange with Ruby's inheritance mechanism At 11:41 PM 6/26/01 +0900, Patrick Logan wrote: >(2) Ruby objects do not have associated "types". How an object can be used > is dependent on the kinds of methods it has, not whether it is a > subclass of this or that superclass. > > For example: > >class ObjectOne > def frobnicate > puts 1 > end >end > >class ObjectTwo > def frobnicate > puts "I am not a number. I am a Church numeral." > end >end > >one = ObjectOne.new >two = ObjectTwo.new > >def doit(something) > something.frobnicate >end > >doit(one) >doit(two) > >Results in: > >1 >I am not a number. I am a Church numeral. > >Even though the two objects are unrelated by class defiinition. Surprisingly enough, this kind of weak typing can be done just as well in C++: class ObjectOne { public: void frobnicate() { cout << 1 << endl; } }; class ObjectTwo { public: void frobnicate() { cout << "I am not a number. I am a Church numeral." << endl; } }; template void doit(T something) { something.frobnicate(); } int main() { ObjectOne one; ObjectTwo two; doit(one) doit(two) return 0; } Results in: 1 I am not a number. I am a Church numeral. The point to me seems to be the nature of variables and bindings, rather than weak typing or the alleged absence of a type for Ruby objects - which by the way is contradicted by class SomeObject; end o = SomeObject.new puts o.class # => SomeObject Bye, Luigi