From: me@... Date: 2011-03-30T05:38:55+09:00 Subject: Re: Why can a floating point number be used as an array index? 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. On Wed, 30 Mar 2011 05:08:36 +0900, Chad Perrin wrote: > On Wed, Mar 30, 2011 at 03:35:21AM +0900, Jeff Dik wrote: >> Why can a floating point number be used as an array index? Anybody >> know of a good use case for this? >> >> irb(main):020:0> [1, 2, 3][0.3] >> 1 >> irb(main):021:0> [1, 2, 3][0.9] >> 1 > > I don't know about why, exactly, or about use cases (though I suppose it > might just make things easier sometimes, without errors cropping up all > over the place if you're working with floating point numbers a lot and > are too lazy to do integer conversions yourself). How seems, from a > little experimenting, to be self-evident to me: > > $ irb > irb(main):001:0> [1,2,3][0.7] > => 1 > irb(main):002:0> [1,2,3][1.7] > => 2 > irb(main):003:0> [1,2,3][1.1] > => 2 > irb(main):004:0> [1,2,3][-0.1] > => 1 > irb(main):005:0> [1,2,3][-1.1] > => 3 > > It looks like it just does integer truncation.