From: Rob Biedenharn Date: 2009-08-05T04:38:33+09:00 Subject: Re: Bizarre Range behavior On Aug 4, 2009, at 3:04 PM, Yossef Mendelssohn wrote: > On Aug 4, 1:47 pm, Scott Briggs wrote: >> Can someone please explain this behavior in ruby (1.8.6p111): >> >>>> ("2"..."8").to_a >> >> => ["2", "3", "4", "5", "6", "7"]>> ("2".."8").to_a >> >> => ["2", "3", "4", "5", "6", "7", "8"]>> ("2".."9").to_a >> >> => ["2", "3", "4", "5", "6", "7", "8", "9"]>> ("2".."10").to_a >> => [] >>>> ("2".."11").to_a >> => [] >>>> ("1".."11").to_a >> >> => ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"] Well, you need to think about String#succ when the Range endpoints are String. > > > It gets better. > >>> ("100".."11").to_a > => ["100"] Now, that one is odd. I'd have predicted a result of: => ["100", "101", "102", "103", "104", "105", "106", "107", "108", "109"] on the basis of staring with "100" and applying #succ until the value was >"11" like this loop does: a = [] v = "100" loop do break if v > "11" a << v v = v.succ end p a This loop produced the "right" result for "2".."11" (namely an empty array) so the actual result defies (my) explanation. > > It seems you're running not so much into strange Range behavior as > strange String behavior in certain numeric circumstances. Or maybe a > combination of strange Range and String behvior. If you want the > ranges to make more sense, use actual numbers. > >>> (2..11).to_a > => [2, 3, 4, 5, 6, 7, 8, 9, 10, 11] > > If you want strings in the result, you can get that with a little bit > of work. > >>> (2..11).to_a.map { |x| x.to_s } > => ["2", "3", "4", "5", "6", "7", "8", "9", "10", "11"] > > -- > -yossef Of course, you can also do things like: ("a".."ah").to_a => ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "aa", "ab", "ac", "ad", "ae", "af", "ag", "ah"] Which might help label your spreadsheet columns. -Rob Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com