[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Idiom of removing a particular character from a String?

Lorenzo E. Danielsson

8/14/2008 2:21:00 PM

Hi all,

Is there any particular idiom for removing a particular character from a
string *by index* and returning the resulting string? The following
makes my old eyes sore:

rem = str.clone
rem[i] = ""
rem

as does this:

rem = str.scan(/./)
rem.delete_at(i)
rem.to_s

It seems there should be some more idiomatic way to do this, something
like str.annihilate_char_at_index_possibly_without_a_bang(i), read as
String#delete_at(i) and String#delete_at!(i). Of course these methods
don't exist, and "".methods doesn't turn up anything interesting. So
what is a *decent* way of performing this?

Lorenzo


11 Answers

Gregory Seidman

8/14/2008 2:40:00 PM

0

On Thu, Aug 14, 2008 at 11:21:10PM +0900, Lorenzo E. Danielsson wrote:
> Is there any particular idiom for removing a particular character from a
> string *by index* and returning the resulting string? The following
> makes my old eyes sore:
>
> rem = str.clone
> rem[i] = ""
> rem
>
> as does this:
>
> rem = str.scan(/./)
> rem.delete_at(i)
> rem.to_s
>
> It seems there should be some more idiomatic way to do this, something
> like str.annihilate_char_at_index_possibly_without_a_bang(i), read as
> String#delete_at(i) and String#delete_at!(i). Of course these methods
> don't exist, and "".methods doesn't turn up anything interesting. So
> what is a *decent* way of performing this?

str[index..index] = ""

> Lorenzo
--Greg


Michael Libby

8/14/2008 2:52:00 PM

0

On Thu, Aug 14, 2008 at 9:21 AM, Lorenzo E. Danielsson
<danielsson.lorenzo@gmail.com> wrote:
> Hi all,
>
> Is there any particular idiom for removing a particular character from a
> string *by index* and returning the resulting string? The following
> makes my old eyes sore:
>
> rem = str.clone
> rem[i] = ""
> rem
>
> as does this:
>
> rem = str.scan(/./)
> rem.delete_at(i)
> rem.to_s
>
> It seems there should be some more idiomatic way to do this, something
> like str.annihilate_char_at_index_possibly_without_a_bang(i), read as
> String#delete_at(i) and String#delete_at!(i). Of course these methods
> don't exist, and "".methods doesn't turn up anything interesting. So
> what is a *decent* way of performing this?

irb(main):022:0> rem = "foobar"
=> "foobar"
irb(main):023:0> rem.slice!(3).chr
=> "b"
irb(main):024:0> rem
=> "fooar"

-Michael

Lorenzo E. Danielsson

8/14/2008 5:00:00 PM

0

On Thu, 2008-08-14 at 23:40 +0900, Gregory Seidman wrote:
> On Thu, Aug 14, 2008 at 11:21:10PM +0900, Lorenzo E. Danielsson wrote:
> > Is there any particular idiom for removing a particular character from a
> > string *by index* and returning the resulting string? The following
> > makes my old eyes sore:
> >
> > rem = str.clone
> > rem[i] = ""
> > rem
> >
> > as does this:
> >
> > rem = str.scan(/./)
> > rem.delete_at(i)
> > rem.to_s
> >
> > It seems there should be some more idiomatic way to do this, something
> > like str.annihilate_char_at_index_possibly_without_a_bang(i), read as
> > String#delete_at(i) and String#delete_at!(i). Of course these methods
> > don't exist, and "".methods doesn't turn up anything interesting. So
> > what is a *decent* way of performing this?
>
> str[index..index] = ""

So:
str = "hello"
rem = str.clone
rem[2,1] = "" # => "helo"
rem

Sorry, but that doesn't look any cleaner than my solution. I was looking
for something like:

class String
def delete_at(i)
str = self.clone
str[i] = ""
end
end

str = "hello"
rem = str.delete_at(2) # => "helo"

That is, original string does not get tampered with. Resulting string is
original string minus exactly one character, at the specified index.

The above works, but I was hoping to avoid that by using some neat
idiom. This is not really a problem, but 99.x % of the time, Ruby does
things in a way that are so cool that I tend to get lazy and expect it
to always behave that way. The few times it doesn't, it comes as a
shock.

>
> > Lorenzo
> --Greg
>
>


Lorenzo E. Danielsson

8/14/2008 5:10:00 PM

0

On Thu, 2008-08-14 at 23:52 +0900, Michael Libby wrote:
> On Thu, Aug 14, 2008 at 9:21 AM, Lorenzo E. Danielsson
> <danielsson.lorenzo@gmail.com> wrote:
> > Hi all,
> >
> > Is there any particular idiom for removing a particular character from a
> > string *by index* and returning the resulting string? The following
> > makes my old eyes sore:
> >
> > rem = str.clone
> > rem[i] = ""
> > rem
> >
> > as does this:
> >
> > rem = str.scan(/./)
> > rem.delete_at(i)
> > rem.to_s
> >
> > It seems there should be some more idiomatic way to do this, something
> > like str.annihilate_char_at_index_possibly_without_a_bang(i), read as
> > String#delete_at(i) and String#delete_at!(i). Of course these methods
> > don't exist, and "".methods doesn't turn up anything interesting. So
> > what is a *decent* way of performing this?
>
> irb(main):022:0> rem = "foobar"
> => "foobar"
> irb(main):023:0> rem.slice!(3).chr
> => "b"
> irb(main):024:0> rem
> => "fooar"

The "problem" is that slice does not return the resulting string but
rather the sliced portion. This is of course not a problem in itself, it
is rather the expected behavior of slice. But, in my particular case
that *still* leads to:

str = "hello"
rem = str.clone
rem.slice!(2)
rem # => "helo"

Whereas I would have wanted:

str = "hello"
rem = str.slice_and_return_everything_but_the_sliced_part(2)

This is partly a matter of laziness on my part, that I prefer to type
two lines instead of four. But, it is also a matter that it kind of
breaks the POLS, at least for me. I have grown accustomed to being able
to perform:

result = original.some_method(args)

and have the result be some mutilated form of original, with original
being left intact. So when String#delete_at(index) doesn't work, it
feels like it *should* have worked..

Anyways, nothing worth losing sleep over..

>
> -Michael
>


ara.t.howard

8/14/2008 5:19:00 PM

0


On Aug 14, 2008, at 8:21 AM, Lorenzo E. Danielsson wrote:

> Hi all,
>
> Is there any particular idiom for removing a particular character
> from a
> string *by index* and returning the resulting string? The following
> makes my old eyes sore:
>
> rem = str.clone
> rem[i] = ""
> rem
>
> as does this:
>
> rem = str.scan(/./)
> rem.delete_at(i)
> rem.to_s
>
> It seems there should be some more idiomatic way to do this, something
> like str.annihilate_char_at_index_possibly_without_a_bang(i), read as
> String#delete_at(i) and String#delete_at!(i). Of course these methods
> don't exist, and "".methods doesn't turn up anything interesting. So
> what is a *decent* way of performing this?
>
> Lorenzo

don't do it by index

string.tr! chars, ''



a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




Yossef Mendelssohn

8/14/2008 5:42:00 PM

0

On Aug 14, 9:21=A0am, "Lorenzo E. Danielsson"
<danielsson.lore...@gmail.com> wrote:
> Hi all,
>
> Is there any particular idiom for removing a particular character from a
> string *by index* and returning the resulting string? The following
> makes my old eyes sore:
>
> rem =3D str.clone
> rem[i] =3D ""
> rem
>
> as does this:
>
> rem =3D str.scan(/./)
> rem.delete_at(i)
> rem.to_s
>
> It seems there should be some more idiomatic way to do this, something
> like str.annihilate_char_at_index_possibly_without_a_bang(i), read as
> String#delete_at(i) and String#delete_at!(i). Of course these methods
> don't exist, and "".methods doesn't turn up anything interesting. So
> what is a *decent* way of performing this?
>
> Lorenzo

Lorenzo,

It seems reading comprehension isn't a strong suit for ruby-talk
today.

As far as I know, there is no such method. You're probably stuck with
reopening String and adding it yourself.

--
-yossef

David Masover

8/15/2008 2:56:00 AM

0

On Thursday 14 August 2008 11:59:51 Lorenzo E. Danielsson wrote:

> So:
> str = "hello"
> rem = str.clone
> rem[2,1] = "" # => "helo"
> rem

For what it's worth, I've been using Object#tap (1.8.7 or higher) in
situations like these:

str = "hello"
str.clone.tap { |rem|
rem[2,1] = ''
}

Ordinarily, I wouldn't mention it, but you were asking for idiomatic things,
and I like that idiom.

Robert Klemme

8/15/2008 12:57:00 PM

0

2008/8/14 Lorenzo E. Danielsson <danielsson.lorenzo@gmail.com>:
> On Thu, 2008-08-14 at 23:52 +0900, Michael Libby wrote:
>> On Thu, Aug 14, 2008 at 9:21 AM, Lorenzo E. Danielsson

> The "problem" is that slice does not return the resulting string but
> rather the sliced portion. This is of course not a problem in itself, it
> is rather the expected behavior of slice. But, in my particular case
> that *still* leads to:
>
> str = "hello"
> rem = str.clone
> rem.slice!(2)
> rem # => "helo"
>
> Whereas I would have wanted:
>
> str = "hello"
> rem = str.slice_and_return_everything_but_the_sliced_part(2)
>
> This is partly a matter of laziness on my part, that I prefer to type
> two lines instead of four. But, it is also a matter that it kind of
> breaks the POLS, at least for me. I have grown accustomed to being able
> to perform:
>
> result = original.some_method(args)
>
> and have the result be some mutilated form of original, with original
> being left intact. So when String#delete_at(index) doesn't work, it
> feels like it *should* have worked..
>
> Anyways, nothing worth losing sleep over..

If you want to do it on one line you could do this:

irb(main):011:0> s = "abcdef"
=> "abcdef"
irb(main):012:0> 5.times {|i| puts s.sub /\A(.{#{i}})./, '\\1' }
bcdef
acdef
abdef
abcef
abcdf
=> 5
irb(main):013:0>

Granted, it's not too idiomatic...

Kind regards

robert

--
use.inject do |as, often| as.you_can - without end

Lorenzo E. Danielsson

8/15/2008 1:43:00 PM

0

On Fri, 2008-08-15 at 11:56 +0900, David Masover wrote:
> On Thursday 14 August 2008 11:59:51 Lorenzo E. Danielsson wrote:
>
> > So:
> > str = "hello"
> > rem = str.clone
> > rem[2,1] = "" # => "helo"
> > rem
>
> For what it's worth, I've been using Object#tap (1.8.7 or higher) in
> situations like these:
>
> str = "hello"
> str.clone.tap { |rem|
> rem[2,1] = ''
> }
>
> Ordinarily, I wouldn't mention it, but you were asking for idiomatic things,
> and I like that idiom.

Agreed, Object#tap looks very nice. Thanks for the info.

Lorenzo


Stefan Rusterholz

8/16/2008 2:10:00 AM

0

Lorenzo E. Danielsson wrote:
> Hi all,
>
> Is there any particular idiom for removing a particular character from a
> string *by index* and returning the resulting string?

str[0...index]+str[index+1..-1]

NB: this won't delete the char from str but only return a new string
without the character at index.

regards
Stefan Rusterholz ("apeiros")
--
Posted via http://www.ruby-....