Project

General

Profile

Misc #10783

String#concat has an "appending" behavior

Added by as-cii (Antonio Scandurra) over 4 years ago. Updated about 1 year ago.

Status:
Open
Priority:
Normal
Assignee:
-
[ruby-core:67813]

Description

Ruby String documentation (http://www.ruby-doc.org/core-2.2.0/String.html) introduces the two terms Appending and Concatenation:

  • Concatenation (aka +) —Returns a new String containing other_str concatenated to str.
  • Append (aka <<) —Concatenates the given object to str.

However, calling concat results in an appending operation. I find this particularly confusing and against the Principle of Least Surprise (e.g. I'd expect concat to actually concatenate something). On the other hand I understand that changing such a small method would result in a quite significant breaking change.

Do you see this as an inconsistency? If yes, is there any particular design (or historical) reason behind it?

Thank you.

P.s. Seems like this is the case for Array as well.

History

Updated by sawa (Tsuyoshi Sawada) over 4 years ago

I can't find the place in the documentation that says "Concatenation (aka +)". Is this your own claim? If so, then that is not correct.

Updated by as-cii (Antonio Scandurra) over 4 years ago

Tsuyoshi Sawada wrote:

I can't find the place in the documentation that says "Concatenation (aka +)". Is this your own claim? If so, then that is not incorrect.

I'll copy over the relevant excerpts.

str + other_str → new_str
Concatenation—Returns a new String containing other_str concatenated to str.
str << integer → str
str << obj → str
Append—Concatenates the given object to str. If the object is a Integer, it is considered as a codepoint, and is converted to a character before concatenation.

But then concat is defined as:

concat(integer) → str
concat(obj) → str
Append—Concatenates the given object to str. If the object is a Integer, it is considered as a codepoint, and is converted to a character before concatenation.

I summarized what I had read in the documentation, therefore it was not an exact quote.

Updated by sawa (Tsuyoshi Sawada) over 4 years ago

I see. You are assimilating the ordinary word "concatenation" with the Ruby method name concat. They are different. But I agree that the documentation is misleading. It should avoid such confusing wording.

Updated by ioquatix (Samuel Williams) about 1 year ago

I also agree that this is confusing, and the only way to fix it would really be to rename concat to append. There is a proposal for something like this here: https://bugs.ruby-lang.org/issues/14033

Also available in: Atom PDF