Hi --

On Mon, 31 Jan 2005, Paul Sanchez wrote:

> In article <20050130152440.A4430 / cs.mcgill.ca>,
> Navindra Umanee <navindra / cs.mcgill.ca> wrote:
>
>> Duck typing is a loose concept however as shown in the previous
>> discussions (an object can be an arbitrary number of types and/or
>> types overlap), and I am not sure if you can call such a thing strong
>> typing although Ruby is type safe within the definition of duck
>> typing.
>>
>> Cheers,
>> Navin.
>
> Hi Navin.,
>
> I think you're making the mistake of equating objects and object
> references.  Objects have a specific type, and the type of an object
> doesn't change.  Variables, on the other hand, are just object
> references.  The object they refer to changes with assignment.  If you
> fire up irb, you can check the type of a given object by sending it the
> "type" message.  For example:
>
> irb(main):001:0> (2**30 - 1).type
> => Fixnum
> irb(main):002:0> (2**30).type
> => Bignum

What version of Ruby are you using?  In recent Rubies you'll get a
warning:

   "".irb(main):001:0> "".type
   (irb):1: warning: Object#type is deprecated; use Object#class
   => String

Hopefully Object#type will disappear completely soon.  My
understanding is that the only reason it ever existed was that there
were problems getting 'class' to parse as a method name (since it's
also a keyword).

It's confusing, because it appears to lend support to a "class ==
type" language model.  But type in Ruby is different from class.  An
object's type is more a matter of its capabilities at a given point in
runtime.  In that sense, the type of an object... well, search the
archives for more on this topic than you will want to read :-)


David

-- 
David A. Black
dblack / wobblini.net