[YANQ] -yet another (ruby) newbie question -string#concat

Hi sir David (aka dblack@candle.superlink.net
[mailto:dblack@candle.superlink.net]):

You remarked last Monday, December 09, 2002 11:30 AM:

String#concat is a synonym for String#<<, and since it
modifies the
object, a concat! version wouldn’t make sense. You can
indeed use
String#+ if you want to create a new string instead of
modifying the
old one.

To be fair here, it might be argued that the String#concat alias is
misnamed and IT doesn’t make sense, given what it does.
Seems to me
(and I’m guessing Botp) that the alias SHOULD be String#concat!.

Not every method that modifies the receiver has a ! – for
example, String#replace. (If there were a replace!, then the
original replace would just be dup :slight_smile: I guess to me
‘concat’ has the same inherently change-rendering implication

Then #replace method is another thing I would avoid since the syntax/naming
has implications or assumptions. Syntax/Naming should be unambiguous as much
as possible, imho… Why can we not standardize on suffixing “!” on methods
that modifies objects? Are they too many? Though, this is not a big problem
for me really, as of now, since I’m actually avoiding them… :slight_smile:

that ‘replace’ does; it sounds like an order is being issued
that something be done to the object.

By way of (merely illustrative) contrast: if there were a method
String#concatted_with(other_string) I would expect that to
generate a new object.

imho, I prefer #concat and #concat!

David

Many thanks, sir David.

kind regards,
-botp

Hi –

Hi sir David (aka dblack@candle.superlink.net
[mailto:dblack@candle.superlink.net]):

You remarked last Monday, December 09, 2002 11:30 AM:

Not every method that modifies the receiver has a ! – for
example, String#replace. (If there were a replace!, then the
original replace would just be dup :slight_smile: I guess to me
‘concat’ has the same inherently change-rendering implication

Then #replace method is another thing I would avoid since the syntax/naming
has implications or assumptions. Syntax/Naming should be unambiguous as much
as possible, imho… Why can we not standardize on suffixing “!” on methods
that modifies objects? Are they too many? Though, this is not a big problem
for me really, as of now, since I’m actually avoiding them… :slight_smile:

Hmmm… I wish I could figure out how to talk you out of that. I
think you’re investing the concept of object-modification with much
more complexity than it has.

As for having all destructive/modifying methods use !: we’d end up
with an awful lot of !'s in our code :slight_smile:

a = [1,2,3,4]
a.push!(5) # and a.push(5) would create a dup of a???
a[-1]! = 6 # as in: a.=!(-1,6)
a.delete_if! {|x| x == 3}

etc., etc…

There’s a lot of previous discussion on this (the general ! question,
not concat specifically). I searched for “destructive modify
receiver” on ruby-talk and hit a few such threads.

David

···

On Mon, 9 Dec 2002, [iso-8859-1] “Peña, Botp” wrote:


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Not every method that modifies the receiver has a ! – for
example, String#replace. (If there were a replace!, then the
original replace would just be dup :slight_smile: I guess to me
‘concat’ has the same inherently change-rendering implication

Then #replace method is another thing I would avoid since the syntax/naming
has implications or assumptions. Syntax/Naming should be unambiguous as much
as possible, imho… Why can we not standardize on suffixing “!” on methods
that modifies objects? Are they too many? Though, this is not a big problem
for me really, as of now, since I’m actually avoiding them… :slight_smile:

Not every destructive method is marked with a ! because …

… I believe the benefit of the “!” notiation is to distinguish between
methods that are similar in semantics but have the subtle difference that one
modifies the recevier and the other does not.

Thus, we are enriched by having:

String#gsub
String#gsub!

Array#uniq
Array#uniq!

Kernel::chomp
Kernel::chomp!

for example.

If we marked every destructive method with “!” then we would lose that
quality about “!” which so distinguishes it as a language feature in the first
place. As David wrote, Array#push! and String#replace! etc. would get a bit
tiresome.

Also note that most library classes do not contain many, if any “!” methods,
because they do not need to make the distinction. A method either modifies the
receiver or it doesn’t. There’s no use having one that does and one that
doesn’t.

“?” methods are used in most Ruby code, though, because there is no distinction
that can possibly be made between “Klass#method” and “Klass#method?”. A method
is either a predicate or it’s not. “?” is just a very nice coding style that
I’m very grateful for.

Gavin

···

From: “Peña, Botp” botp@delmonte-phil.com