In article <990979671.433370.24481.nullmailer / ev.netlab.zetabits.com>,
Yukihiro Matsumoto <matz / zetabits.com> wrote:
>|
>|And then I would like Ruby to notice that it has the to_i method
>|defined, and so automatically call this when it needs it to act like
>|an integer.
>
>Try 'to_int' which is for that particular purpose, not 'to_i' which is
>for explicit conversion to integer.  And when you want something to be
>string, try 'to_str'.

Thank you for your help.  I also should have been more specific about
the version of Ruby I was running; as it turned out, I had an older
version of Ruby (1.6.1) which didn't support to_int like that.  When I
moved to the latest stable Ruby, (1.6.3), it worked as you stated:

	class MyClass < OtherClass
	  def to_int
	    3
	  end
	end

	a = [ "cat", "dog", "fish", "horse", "mouse" ]
	m = MyClass.new
	a[m]
	   "horse"

This is great!  However, I notice that when I try to use MyClass
objects in addition, I get these errors:

	m + 4
	ERR: (eval):1: undefined method `+' for #<MyClass:0x401787a0>

	4 + m
	ERR: (eval):1:in `+': MyClass can't be coerced into Fixnum

I'm not exactly sure what I need to do to make this work.

regards,
--Mirian