From: Dave Thomas Date: 2003-03-24T16:33:35+09:00 Subject: Re: Types (was Re: Ruby lecture slides (was Strong advantages over Python)) On Sunday, Mar 23, 2003, at 23:56 US/Central, Mark Wilson wrote: > Ruby has types. In Ruby, they are generally called classes. > Conceptually, there should not be a distinction between type and class > and this is certainly true of Ruby. > The first sentence is true, but the second is only a convention. Take this example: class Fred def shout puts "wilma" end end f = Fred.new o = Object.new def o.shout puts "wilma" end 'f' and 'o' have different classes (Fred and Object respectively) but they have indistinguishable types (for all intents and purposes). In general, types and classes are only loosely connected in Ruby, which is one of the languages strengths. For example, I recently wrote a search engine that initially stored a vector of bits in an Array. I was able to change the code to store the bits in a Bignum instead by changing just two lines of code (and one of those was because I was being lazy in the original implementation). For the subset of operations I used, Bignum and Array had the same type. This more commonly occurs with operators such as '<<' : we quite commonly write code where we don't care what class something is: we just need to be able to append to it. That's the essence of duck typing: if something walks and talks like a duck, then it may as well be a duck. Cheers Dave