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…
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.
In message “Re: String#insert method is destructive… inconsistent?”
on Fri, 15 Dec 2006 07:48:25 +0900, Joe P. [email protected]
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.
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…!
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).
In message “Re: String#insert method is destructive… inconsistent?”
on Fri, 15 Dec 2006 07:48:25 +0900, Joe P. [email protected]
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.).