From: nobu@... Date: 2015-12-09T07:51:18+00:00 Subject: [ruby-core:71989] [Ruby trunk - Feature #11781] Would it be possible to alias .prepend() towards .unshift() for class Array by default? Issue #11781 has been updated by Nobuyoshi Nakada. Their arities differ. Let `String#prepend` take any number of arguments? ~~~diff diff --git i/array.c w/array.c index dd14837..b98142d 100644 --- i/array.c +++ w/array.c @@ -5832,6 +5832,7 @@ Init_Array(void) rb_define_method(rb_cArray, "pop", rb_ary_pop_m, -1); rb_define_method(rb_cArray, "shift", rb_ary_shift_m, -1); rb_define_method(rb_cArray, "unshift", rb_ary_unshift_m, -1); + rb_define_method(rb_cArray, "prepend", rb_ary_unshift_m, -1); rb_define_method(rb_cArray, "insert", rb_ary_insert, -1); rb_define_method(rb_cArray, "each", rb_ary_each, 0); rb_define_method(rb_cArray, "each_index", rb_ary_each_index, 0); diff --git i/string.c w/string.c index e6df91d..d0f6454 100644 --- i/string.c +++ w/string.c @@ -2691,21 +2691,28 @@ rb_str_concat(VALUE str1, VALUE str2) /* * call-seq: - * str.prepend(other_str) -> str + * str.prepend(other_str, ...) -> str * * Prepend---Prepend the given string to str. * * a = "world" * a.prepend("hello ") #=> "hello world" * a #=> "hello world" + * + * a = "world" + * a.prepend("hello", " ") #=> "hello world" + * a #=> "hello world" */ static VALUE -rb_str_prepend(VALUE str, VALUE str2) +rb_str_prepend(int argc, VALUE *argv, VALUE str) { - StringValue(str2); - StringValue(str); - rb_str_update(str, 0L, 0L, str2); + int i; + for (i = argc; i > 0;) { + VALUE str2 = argv[--i]; + StringValue(str2); + rb_str_update(str, 0L, 0L, str2); + } return str; } @@ -9379,7 +9386,8 @@ Init_String(void) rb_define_method(rb_cString, "reverse!", rb_str_reverse_bang, 0); rb_define_method(rb_cString, "concat", rb_str_concat, 1); rb_define_method(rb_cString, "<<", rb_str_concat, 1); - rb_define_method(rb_cString, "prepend", rb_str_prepend, 1); + rb_define_method(rb_cString, "prepend", rb_str_prepend, -1); + rb_define_method(rb_cString, "unshift", rb_str_prepend, -1); rb_define_method(rb_cString, "crypt", rb_str_crypt, 1); rb_define_method(rb_cString, "intern", rb_str_intern, 0); /* in symbol.c */ rb_define_method(rb_cString, "to_sym", rb_str_intern, 0); /* in symbol.c */ diff --git i/test/ruby/test_string.rb w/test/ruby/test_string.rb index eed7c69..853ef7c 100644 --- i/test/ruby/test_string.rb +++ w/test/ruby/test_string.rb @@ -2194,6 +2194,14 @@ a.prepend(b) assert_equal(S("hello world"), a) assert_equal(S("hello "), b) + + a = S("world") + b = S("hel") + c = S("lo ") + a.prepend(b, c) + assert_equal(S("hello world"), a) + assert_equal(S("hel"), b) + assert_equal(S("lo "), c) end def u(str) ---------------------------------------- Feature #11781: Would it be possible to alias .prepend() towards .unshift() for class Array by default? https://bugs.ruby-lang.org/issues/11781#change-55399 * Author: Robert A. Heiler * Status: Open * Priority: Normal * Assignee: ---------------------------------------- Hello. For Strings we can do: abc = 'world!' abc[0,0] = 'Hello ' abc # => "Hello world!" For Arrays we can do: abc = ['world!'] abc[0,0] = 'Hello ' abc # => ["Hello ", "world!"] This is nice. For Strings we can also use .prepend() to add to the beginning. For Arrays, we have to use .unshift(). I have a hard time remembering .unshift though, .prepend() seems to be easier for me to remember. I'd like to use both .prepend for Strings and Arrays; right now I have to use different names. I could alias prepend to unshift for class Array, but then I'd have to carry these modifications into my projects, which is not so good - I would prefer to just stick to what MRI is doing. Could we have the alias .prepend() for class Array, meaning .unshift() too? That way I could use .prepend() for both Arrays and Strings. Thanks for reading! -- https://bugs.ruby-lang.org/