String#insert method is destructive... inconsistent?

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-forum.com/.

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-talk/5736

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-forum.com/\.

Hi,

···

In message "Re: String#insert method is destructive... inconsistent?" on Fri, 15 Dec 2006 07:48:25 +0900, Joe Peterson <joe@lavajoe.com> writes:

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

concat is destructive as well.

              matz.

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-talk/5736

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-forum.com/\.

Yukihiro Matsumoto wrote:

Hi,

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

concat is destructive as well.

              matz.

Thanks, Matz - so cool to get a quick reply! I guess it was the things
like "sub", "gsub", "delete", etc. (all having both forms) that tripped
me up, especially since "insert" seemed like an inverse of "delete".
"concat", since it is like the "<<" operator (as well as "replace", like
the "=" operator), didn't catch my eye in the same way. Is "insert"
fundamentally different than "delete" based on the two rules in the link
provided in the previous post? Ah, I'm probably splitting hairs, being
a perfectionist...!

-LavaJoe

···

In message "Re: String#insert method is destructive... inconsistent?" > on Fri, 15 Dec 2006 07:48:25 +0900, Joe Peterson <joe@lavajoe.com> > writes:

--
Posted via http://www.ruby-forum.com/\.

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-forum.com/\.

Hi --

···

On Fri, 15 Dec 2006, Joe Peterson wrote:

I have read the whole thread - very interesting, and it's from 2000!

Interesting things being said...six years ago?! Will wonders never
cease?

:slight_smile: I feel very old on this list sometimes :slight_smile:

David

--
Q. What's a good holiday present for the serious Rails developer?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black\)
    aka The Ruby book for Rails developers!
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)

You are. :slight_smile:

James Edward Gray II

···

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

:slight_smile: I feel very old on this list sometimes :slight_smile: