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

Hello Ruby Friends,

From now on, I’ll prefix my questions w YANQ (yet another (ruby) newbie
question) so it would be easier for you to delete (in case :-).

My YANQotd is regarding string#concat. A sample code and output would be
good to illustrate:

C:\family\ruby>type a1.rb
fn = "bot"
ln = "pen"
name = fn.concat(ln)

print "first name: ", fn
puts
print "last name : ", ln
puts
print "full name : ", name

C:\family\ruby>ruby a1.rb
first name: botpen
last name : pen
full name : botpen

I noticed that concat modified fn. I looked for other concat methods but
couldn’t find one (but the + op). I was hoping that there would be a concat!
and concat pair (like chop!/chop and chomp!/chomp).

Maybe string#concat! was deprecated for “+”? My concern is that (as a
newbie), I’m trying to avoid methods with “!” (like chomp!/chop!) so I have
less to worry/know. My strategy was working and I was actually coding w
breeze… until I bump… :frowning:

Pls enlighten.

kind regards,
-botp

My YANQotd is regarding string#concat. A sample code and output would be
good to illustrate:

[snip]

I noticed that concat modified fn. I looked for other concat methods but
couldn’t find one (but the + op). I was hoping that there would be a
concat!
and concat pair (like chop!/chop and chomp!/chomp).

Maybe string#concat! was deprecated for “+”? My concern is that (as a
newbie), I’m trying to avoid methods with “!” (like chomp!/chop!) so I
have
less to worry/know. My strategy was working and I was actually coding w
breeze… until I bump… :frowning:

I think that the operation “concatenation” can be
viewed in two ways.

The first (as I would tend to view it) is a simple
operation that takes two operands and gives a result
(same as +).

The other is an operation on a certain object (like
append). This is how String#concat works; it is an
append operation.

Note that this is almost as much as question of English
usage (IMO) as of computer science.

If strings were immutable, there would be no question
of what is meant here; but they are mutable.

As for avoiding the “!” methods… I wouldn’t suggest
that. Do what works for you, but I would suggest just
learning the two ways of thinking and using each when
appropriate. I.e., “Should I modify the original object
or make a new one?”

Just my thoughts.

Hal

···

----- Original Message -----
From: “Peña, Botp” botp@delmonte-phil.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Sunday, December 08, 2002 8:14 PM
Subject: [YANQ] -yet another (ruby) newbie question -string#concat

Hi –

Hello Ruby Friends,

From now on, I’ll prefix my questions w YANQ (yet another (ruby) newbie
question) so it would be easier for you to delete (in case :-).

ruby + newbie == nuby :slight_smile:

My YANQotd is regarding string#concat. A sample code and output would be
good to illustrate:

C:\family\ruby>type a1.rb
fn = “bot”
ln = “pen”
name = fn.concat(ln)

print "first name: ", fn
puts
print "last name : ", ln
puts
print "full name : ", name

C:\family\ruby>ruby a1.rb
first name: botpen
last name : pen
full name : botpen

I noticed that concat modified fn. I looked for other concat methods but
couldn’t find one (but the + op). I was hoping that there would be a concat!
and concat pair (like chop!/chop and chomp!/chomp).

Maybe string#concat! was deprecated for “+”? My concern is that (as a

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.

newbie), I’m trying to avoid methods with “!” (like chomp!/chop!) so I have
less to worry/know. My strategy was working and I was actually coding w
breeze… until I bump… :frowning:

I think in the long run that strategy will give you more to worry
about :slight_smile: Don’t feel you have to avoid ! methods; they’re a normal
part of Ruby, and not particularly complicated.

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

print "first name: ", fn
puts
print "last name : ", ln
puts
print "full name : ", name

Replace that with

puts "first name: " + fn
puts "last name : " + ln
puts "full name : " + name

for clarity and ease of coding.

Cheers,
Gavin

···

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

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!.

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!.

I’d agree with that, FWIW. IMO, botp’s confusion is quite understandable. I
never use #concat, but if I did I’m sure I would have raised this issue before.

I see no reason why str1.concat(str2) should modify str1, any more than the
Unix command “cat file1 file2” should modify file1 (it doesn’t).

The Mirriam-Webster online dictionary lists “concatenate [v.t.]” as

to link together in a series or chain

This suggests that String#concat should indeed be destruvtive. However, since
there is ample room for confusion, and there are many String#xxx! methods, I
conclude that the ideal solution is for String to have the following methods:

String#concat
String#concat!

with their operations being (now) obvious.

I wonder how much existing code such a change will break. After all, who, in a
published program or library, would use #concat ? :slight_smile:

Cheers,
Gavin

···

From: “Mike Campbell” michael_s_campbell@yahoo.com

Hi –

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 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.

David

···

On Mon, 9 Dec 2002, Mike Campbell wrote:


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

I wonder how much existing code such a change will break. After all, who, in
a
published program or library, would use #concat ? :slight_smile:

I meant “… such a change would break …”. I don’t expect it to be changed
any time soon.

Gavin

···

From: “Gavin Sinclair” gsinclair@soyabean.com.au

Hi –

I see no reason why str1.concat(str2) should modify str1, any more than the
Unix command “cat file1 file2” should modify file1 (it doesn’t).

But the semantics are very different. The Unix semantics are very
“top level”, looking down equally at both files. Moreover, one could
argue that the “receiver” here is STDOUT, which is indeed being
modified.

The Mirriam-Webster online dictionary lists “concatenate [v.t.]” as

to link together in a series or chain

This suggests that String#concat should indeed be destruvtive. However, since
there is ample room for confusion, and there are many String#xxx! methods, I
conclude that the ideal solution is for String to have the following methods:

String#concat
String#concat!

with their operations being (now) obvious.

Or not :slight_smile: String#concat! sounds redundant to me (see my other
post).

I wonder how much existing code such a change will break. After
all, who, in a published program or library, would use #concat ? :slight_smile:

/usr/local/src/ruby$ grep \.concat find . -name "*.rb" | wc -l
43

(Of course some of those are Array#concat, but a lot are strings.)

David

···

On Mon, 9 Dec 2002, Gavin Sinclair wrote:


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