From: Robert Klemme Date: 2009-05-10T03:00:30+09:00 Subject: Re: ruby string slice/[] w/ range, weird end behavior On 09.05.2009 02:19, Gary Yngve wrote: > On May 8, 4:47 pm, Eleanor McHugh > wrote: >> "foo"[3..3] => "" > .. >> index [3] is actually the end of the string so whilst the range >> accesses the string after any characters in it, it's still accessing >> the string in a valid range. > > Maybe in the native C. But why should that be exposed? > And why shouldn't index[3] return "" if you are correct? If we change perspective a bit, the behavior seems pretty naturally to me: if you execute this: s = "foo" l = s.length (l + 2).times do |i| p i, s[i,l - i], s[i, 1 + l - i] end you get this: 0 "foo" "foo" 1 "oo" "oo" 2 "o" "o" 3 "" "" 4 nil nil In this context, returning the empty string for 3,0 seems ok - especially if you consider, that s[a,b] is truncated at the end of the string if a + b > s.length. Kind regards robert