From: Charlie Date: 2006-06-11T21:08:47+09:00 Subject: Re: Substrings by Position. Robert Klemme wrote: > 2006/6/11, Charlie : >> Hi, >> >> Just a logic question about substrings by position. >> >> foo = "012345678" >> foo[5,1] #-> "5" >> foo[-4,2] #-> "56" >> foo[-3,2] = "" #-> "0123458" > > What version are you on? 1.8.4 You have probably missed out something, its foo[-3,2] = "" not foo[-3,2] > >>> foo = "012345678" > => "012345678" >>> foo[-3,2] > => "67" >>> RUBY_VERSION > => "1.8.3" > > Note also that you can use a range as well: > >>> foo[-3..-1] > => "678" >>> foo[-3...-1] > => "67" > >> Shouldn't a negative indexing work as well? (Below are just examples, >> they currently don't work) It will allow strings to be REVERSED easily, >> and thus doing something new. > > Negative indexes do work already. You probably mean negative lengths. Yes, you're right. It just mean lengths with direction. > >> foo = "012345678" >> foo[4,1] #-> 4 >> foo[4,-1] #-> 3 >> >> Is this making sense? > > Dunno. I personally never missed this behavior. I'm not sure whether > there is much use for this and if it's worth the effort. Note that the > implementation of String#[] becomes even more complex than it is right > now plus with the current design returned strings usually share the > buffer with the original. You cannot do that with implicit reversed > strings. I opt for a mild "no" but I'm not religious. I just thought that if the current behaviour is so useful, then it'll be equally useful to have an equal and opposite behaviour. It's just unnatural that things can only go one direction. Just a thought. > > Kind regards > > robert -- Posted via http://www.ruby-forum.com/.