Project

General

Profile

Actions

Feature #12333

closed

`String#concat`, `Array#concat`, `String#prepend` to take multiple arguments

Added by sawa (Tsuyoshi Sawada) over 8 years ago. Updated about 8 years ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:75264]

Description

I would like String#concat, Array#concat, String#prepend to take multiple arguments

s = ""
s.concat("a", "b", "c")
a.prepend("A", "B", "C")
s # => "ABCabc"

a = []
a.concat(["a"], ["b", "c"])
a # => ["a", "b", "c"]

Files

multi_concat_prepend.patch (2.94 KB) multi_concat_prepend.patch spinute (Satoru Horie), 05/03/2016 07:14 AM
fixed_multi_concat_prepend.patch (3.14 KB) fixed_multi_concat_prepend.patch spinute (Satoru Horie), 05/03/2016 08:03 AM
multi_concat_prepend.patch (7.19 KB) multi_concat_prepend.patch diff from: 0052ed9d806f3739df9200891a376a18dc8068f9 spinute (Satoru Horie), 06/20/2016 03:31 PM

Related issues 2 (1 open1 closed)

Related to Ruby master - Feature #12247: accept multiple arguments at Array#deleteOpenActions
Related to Ruby master - Bug #13268: [DOC] Restore docs for String#<<Closedstomar (Marcus Stollsteimer)Actions

Updated by duerst (Martin Dürst) over 8 years ago

I think this can occasionally be helpful, and shouldn't be too difficult to implement. Can you provide a patch?

Updated by spinute (Satoru Horie) over 8 years ago

I will try to write a patch for it!

Updated by spinute (Satoru Horie) over 8 years ago

I have written a patch.

And, there are some points to ask

  • What is the appropriate behavior when calling concat/prepend without argument?

  • The code attached now returns just self

  • What should happen when writing ar.concat(ar, ar)?

  • ar = [1]; ar.concat(ar, ar) #=> [1,1,1]? or [1,1,1,1]?

  • ar << ar << ar returns [1,1,1,1], of course because this is just a sequence of binary operations

  • However, I feel "ar.concat(ar, ar)" saying "append present content of the array twice to the array", meaning [1,1,1]

  • The code attached returns [1,1,1,1], for now, just for the simplicity of implementation

Updated by spinute (Satoru Horie) over 8 years ago

I wrote another patch refined for a problem stated in a previous post.

It behaves like below

ar = [1]
ar.concat(ar, ar) #=> [1,1,1]

str = "ab"
str.concat(str, str) #=> "ababab"

Updated by sawa (Tsuyoshi Sawada) over 8 years ago

Satoru, thank you for the patch.

And you are making good points with the questions you raised.

  • What is the appropriate behavior when calling concat/prepend without argument?
  • The code attached now returns just self

I agree with this behavior. I think this would be the most natural.

  • What should happen when writing ar.concat(ar, ar)?
  • ar = [1]; ar.concat(ar, ar) #=> [1,1,1]? or [1,1,1,1]?
  • ar << ar << ar returns [1,1,1,1], of course because this is just a sequence of binary operations

This is more logical (in some sense), but counter-intuitive.

  • However, I feel "ar.concat(ar, ar)" saying "append present content of the array twice to the array", meaning [1,1,1]

This is more intuitive, but may be less logical.

I have preference with the second option, but am not completely sure. Maybe other people might have different opinions.

Actions #6

Updated by hsbt (Hiroshi SHIBATA) over 8 years ago

  • Related to Feature #12247: accept multiple arguments at Array#delete added

Updated by matz (Yukihiro Matsumoto) over 8 years ago

Approved. I want to

ar = [1]
ar.concat(ar, ar)

to result [1,1,1].

Matz.

Updated by spinute (Satoru Horie) over 8 years ago

I added some test cases for Array#concat, String#concat and String#prepend and refined error handling.
Also, I fixed my code to conform to convention.
Any feedback is welcome!

Actions #9

Updated by nobu (Nobuyoshi Nakada) about 8 years ago

  • Status changed from Open to Closed

Applied in changeset r56021.


multiple arguments

  • array.c (rb_ary_concat_multi): take multiple arguments. based
    on the patch by Satoru Horie. [Feature #12333]
  • string.c (rb_str_concat_multi, rb_str_prepend_multi): ditto.
Actions #10

Updated by stomar (Marcus Stollsteimer) over 7 years ago

  • Related to Bug #13268: [DOC] Restore docs for String#<< added
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0