Project

General

Profile

Feature #12333

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

Added by sawa (Tsuyoshi Sawada) over 1 year ago. Updated over 1 year ago.

Status:
Closed
Priority:
Normal
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"]
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

Related to Ruby trunk - Feature #12247: accept multiple arguments at Array#deleteOpen
Related to Ruby trunk - Bug #13268: [DOC] Restore docs for String#<<Closed

Associated revisions

Revision 56021
Added by nobu (Nobuyoshi Nakada) over 1 year ago

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.

Revision 56021
Added by nobu (Nobuyoshi Nakada) over 1 year ago

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.

History

#1 [ruby-core:75294] Updated by duerst (Martin Dürst) over 1 year ago

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

#2 [ruby-core:75299] Updated by spinute (Satoru Horie) over 1 year ago

I will try to write a patch for it!

#3 [ruby-core:75318] Updated by spinute (Satoru Horie) over 1 year 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

#4 [ruby-core:75319] Updated by spinute (Satoru Horie) over 1 year 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"

#5 [ruby-core:75320] Updated by sawa (Tsuyoshi Sawada) over 1 year 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.

#6 Updated by hsbt (Hiroshi SHIBATA) over 1 year ago

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

#7 [ruby-core:75968] Updated by matz (Yukihiro Matsumoto) over 1 year ago

Approved. I want to

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

to result [1,1,1].

Matz.

#8 [ruby-core:76087] Updated by spinute (Satoru Horie) over 1 year 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!

#9 Updated by nobu (Nobuyoshi Nakada) over 1 year 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.

#10 Updated by stomar (Marcus Stollsteimer) 9 months ago

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

Also available in: Atom PDF