From: Adam Shelly Date: 2006-01-20T15:06:22+09:00 Subject: Re: Ruby Quiz #62 On 1/19/06, James Edward Gray II wrote: > On Jan 19, 2006, at 8:06 PM, Adam Shelly wrote: > > irb(main):013:0> r=1...9 > > => 1...9 > > irb(main):014:0> r.last > > => 9 > > irb(main):015:0> r.include? r.last > > => false > > > > I really expected (1...9).last to return 8. I guess this is just > > another way ranges are screwy. > > Na, that's not odd at all. Just tell yourself .. is inclusive > and ... is exclusive (with regards to the last member). See how that > extra dot pushes it off the end there... ;) > That part I know. But I expected (1...x) to be exactly equivalent to (1..x-1) The difference bit me when I was trying to write a version of rand that took a range (It would be nice if that was a built-in...). My first attempt is below, but it returns the wrong results when rng is an exclusive range. def rrand rng rng.first + rand(rng.last-rng.first) end -Adam