String#insert method is destructive... inconsistent?

J

Joe Peterson

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
 
J

Joe Peterson

Marcel said:
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
 
J

Joe Peterson

Yukihiro said:
Hi,

In message "Re: String#insert method is destructive... inconsistent?"
|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
 
J

Joe Peterson

Joe said:
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
 
D

dblack

Hi --

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

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

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


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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,607
Members
45,240
Latest member
pashute

Latest Threads

Top