Florian Kaufmann wrote:

> The documentation of the standard classes/methods never mention of
> what type/class an argument must be. For example let's look at the
> subscript of an Array. There is the form "array[index]". But of what
> Type must the index object be?
> The ruby book tells me that programming ruby, types doesn't matter so
> much, what matters is to what messages an object responds. Thus I
> tried the following, thinking that to_i is needed so an object can be
> used as an subscript index.
> 
> #!/usr/bin/ruby
> class C
>   def to_i
>     0
>   end
> end
> c = C.new
> a = [0,1,2]
> p a[c]
> 
> It doesn't work however:
> 
> ./test:12:in `[]': can't convert C into Integer (TypeError)
> from ./test:12
> 
> So to which messages must C respond so objects of it can be used as
> array index?

An index is always a natural number. It is required to be either a Fixnum or 
Bignum. You can't pass arbitrary object to those methods. Why not:

  p a[c.to_i] ?

Regards,

  Michael