From: moontoeki@... (Sung Moon) Date: 2002-02-04T13:17:26+09:00 Subject: Re: range and modification >From: Jeff 'japhy' Pinyan jeffp@crusoe.net >On Feb 4, Sung Moon said: > >>n[4..8].sort! doesn't work(same for compact!, collect!) > >That's because > > a = b[4..8] > >doesn't make a[0] through a[5] "aliases" to b[4] through b[8]. > > b = (0..19).to_a > a = b[4 .. 8] > a[0] = 99 > puts b.join(" ") > >You'll see that b[4] is still 4, not 99. > >>Is there any elegant way to do this job? > > n[4..8] = n[4..8].sort > >is my best suggestion. > It's so simple. I thouht in hard way. >>2. It seems LAST line in text does not have \n or \r\n, >> (I used Wordpad for creating this text) >> so I can't use chop/chomp without losing last digit/character. > >Uh, you can use chomp() safely. > > ["jeff\n", "japhy"].each{|str| puts "<#{ str.chomp }>"} > # and > ["jeff\n", "japhy"].each{|str| puts "<#{ str.chomp.inspect }>"} ==> <"jeff"> <"japhy"> It's OK ["jeff\n", "japhy"].each{|str| puts "<#{ str.chop.inspect }>"} ==> <"jeff"> <"japh"> It's not OK ["jeff\r\n", "japhy"].each{|str| puts "<#{ str.chomp.inspect }>"} ==> <"jeff\r"> <"japhy"> It's not OK ["jeff\r\n", "japhy"].each{|str| puts "<#{ str.chop.inspect }>"} ==> <"jeff"> <"japh"> It's not OK So, we can use chomp for the line ending with \n, but we can not use either chop or chomp for line ending with \r\n. sungjin moon