From: James Edward Gray II Date: 2007-10-12T21:38:12+09:00 Subject: Re: How to delete specific characters from a string? On Oct 11, 2007, at 11:00 PM, mortee wrote: > Wayne E. Seguin wrote: >> On 10/11/07, Giles Bowkett wrote: >>> I think the OP was looking for a method on String itself, but the >>> whole point of Ruby is that if the language doesn't have the >>> features >>> you want, you just add the features to the language. >>> >>> class String >>> def delete_n_from_p(n, p) >>> n.times do >>> self[p] = '' >>> end >>> self >>> end >>> end >>> >>>>> "muppet".delete_n_from_p(2,3) >>> => "mupt" >>> >>> That makes it easy to reuse the functionality. >>> >>> -- >>> Giles Bowkett >>> >> >> >> This little problem is quite enjoyable: >> >> class String >> def delete_indices(*indices) >> indices.each do |index| >> self[index] = '' >> end >> self >> end >> end >> >>>> a = "Testing String" >> => "Testing String" >>>> a.delete_indices(0,3,6,8) >> => "estng trng" > > What's your point? Your code probably won't do what it seems to intend > to, because the characters shift to the left during the process. You can fix the ordering issue above by changing the line: indices.each do |index| to: indices.sort { b <=> a }.each do |index| Fixing for range usage is more complicated though. James Edward Gray II