From: Robert Klemme Date: 2011-03-30T18:15:19+09:00 Subject: Re: Why can a floating point number be used as an array index? On Tue, Mar 29, 2011 at 10:38 PM, wrote: > It may do integer truncation, but we need to think of what an Array > actually is as far as the structure goes. What would one expect to get out > of [0,1,2][0.3] ? Should it be 30% of the first element? That doesn't seem > right. With arrays, you are either in one element or not. Integers make > more sense because they represent the location you're in. Either the 0 > spot, 1 spot, or 2 spot. Think of a row of lockers at school. You can't be > partially between each locker. It makes more sense to only use one locker > or the next. Yes, of course. That's why Array#[] uses #to_int to determine whether the index is integerish. The question to ask would be whether Float should be "integerish" - Matz decided "yes" a long time ago and from what I can recall there were not much complaints over time. From a formal (or mathematical) point of view it's rather the other way round: an integer /is a/ float but a float /is not an/ integer. But Ruby is a pragmatic language and so I believe they figured that it would be more convenient to have Float#to_int than not. YMMV though and you can easily overwrite Float#to_int to throw. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/