[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

String#insert method is destructive... inconsistent?

Joe Peterson

12/14/2006 10:48:00 PM

Hi all,

I am relatively new to Ruby, and I am loving it! I have a long Python
background, and one of the things I love about Ruby is that it is easier
to know and even remember which methods are destructive and which just
return a result.

Looking through the "String" class methods, one sticks out: "insert".

Almost all others (and all that might be ambiguous), like "delete",
"sub", etc., have both the non-destructive and destructive versions
("delete", "delete!", etc.).

Does anyone know the reason "insert" isn't this way? It seems so
similar to delete that I would have expected it not to change the string
unless a "!" were at the end, but it looks like there is just the
non-"!" one, and it does change the string...

Thanks, LavaJoe

--
Posted via http://www.ruby-....

5 Answers

Marcel Ward

12/15/2006 12:05:00 AM

0

I also started wondering...

Then a quick google for: ruby-talk insert destructive

... gives a reasonable explanation on the first hit:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby...

Marcel

On 14/12/06, Joe Peterson <joe@lavajoe.com> wrote:
> Hi all,
>
> I am relatively new to Ruby, and I am loving it! I have a long Python
> background, and one of the things I love about Ruby is that it is easier
> to know and even remember which methods are destructive and which just
> return a result.
>
> Looking through the "String" class methods, one sticks out: "insert".
>
> Almost all others (and all that might be ambiguous), like "delete",
> "sub", etc., have both the non-destructive and destructive versions
> ("delete", "delete!", etc.).
>
> Does anyone know the reason "insert" isn't this way? It seems so
> similar to delete that I would have expected it not to change the string
> unless a "!" were at the end, but it looks like there is just the
> non-"!" one, and it does change the string...
>
> Thanks, LavaJoe
>
> --
> Posted via http://www.ruby-....
>
>

Joe Peterson

12/15/2006 12:33:00 AM

0

Marcel Ward wrote:
> I also started wondering...
>
> Then a quick google for: ruby-talk insert destructive
>
> ... gives a reasonable explanation on the first hit:
>
> http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby...
>
> Marcel

Ah, thanks for the link. I tried Googling for "ruby String#insert
non-destructive" and variants like that (just didn't do "ruby-talk"). I
saw a few hits that mentioned insert, etc., but nothing that addressed
my pondering.

-LavaJoe

--
Posted via http://www.ruby-....

Joe Peterson

12/15/2006 1:46:00 AM

0

Joe Peterson wrote:
> Ah, thanks for the link. I tried Googling for "ruby String#insert
> non-destructive" and variants like that (just didn't do "ruby-talk"). I
> saw a few hits that mentioned insert, etc., but nothing that addressed
> my pondering.
>
> -LavaJoe

Sorry for so many posts... But I realized that the search I had done
actually found a different message within the same thread mentioned
above, and the thread talks about the same issue, but in the "Array"
class. There, both "insert" and "delete" are destructive and have no
"!" versions (it seems that "!" is used more sparingly in Array), so I
did not think that the thread addressed my concern at first glance.

I have read the whole thread - very interesting, and it's from 2000! It
appears that how one decides to define a method is largely based upon
that person's impression of the meaning of the method (in other words,
the "feeling" the method invokes, which may not invoke quite the same
feeling in others).

-LavaJoe

--
Posted via http://www.ruby-....

dblack

12/15/2006 2:00:00 AM

0

James Gray

12/15/2006 2:05:00 AM

0

On Dec 14, 2006, at 8:00 PM, dblack@wobblini.net wrote:

> :-) I feel very old on this list sometimes :-)

You are. :)

James Edward Gray II