raja / cs.indiana.edu (Raja S.) writes:

> Interesting that Ruby does a #to_s on the elements of the array
> first.

Actually, what happens is that when you concatenate something to an
existing string, if that something isn't already a string, its to_str
method is called.

   "a" + 1
   -:1:in `+': failed to convert Fixnum into String (TypeError)
	  from -:1

   class Fixnum
      def to_str
        "Number: " + to_s
      end
   end

   "a" + 1     #=> "aNumber: 1"


> Too bad that such a crucial issue as exception raising for
> out-of-bounds indexing in an array (or hash) got left out.  As, Dave
> suggests, I'm curious to try overloading [], []= to see if/what
> breaks.

Before you do that, though, can I suggest an experiment?

Do nothing. Just try it as it is, and see what happens. See how often
you get bitten by this. Perhaps adjust your coding style slightly to
adapt to a new language.

After a while, see what you think. Perhaps you'll have some horror
stories that will convince Matz to add IndexError exceptions. Or
perhaps you'll conclude it isn't necessary, and that perhaps it wasn't
so much "left out" as "designed out" ;-)


Regards


Dave