James Gray
10/12/2007 12:33:00 PM
On Oct 11, 2007, at 9:30 PM, Giles Bowkett wrote:
>>>> Is there really no method that allows me to delete N characters
>>>> starting
>>>> at position P from a string? I have looked (carefully I hope)
>>>> through
>>>> the String methods and did not see a way to do this. Thanks.
>
> 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.
>> s = "muppet"
=> "muppet"
>> s[3, 2] = ""
=> ""
>> s
=> "mupt"
>>
James Edward Gray II