ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:71897] [Ruby trunk - Feature #11781] [Open] Would it be possible to alias .prepend() towards .unshift() for class Array by default?
       [not found] <redmine.issue-11781.20151207101222@ruby-lang.org>
@ 2015-12-07 10:12 ` shevegen
  2015-12-07 11:13 ` [ruby-core:71901] [Ruby trunk - Feature #11781] " 6ftdan
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: shevegen @ 2015-12-07 10:12 UTC (permalink / raw
  To: ruby-core

Issue #11781 has been reported by Robert A. Heiler.

----------------------------------------
Feature #11781: Would it be possible to alias .prepend() towards .unshift() for class Array by default?
https://bugs.ruby-lang.org/issues/11781

* 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/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [ruby-core:71901] [Ruby trunk - Feature #11781] Would it be possible to alias .prepend() towards .unshift() for class Array by default?
       [not found] <redmine.issue-11781.20151207101222@ruby-lang.org>
  2015-12-07 10:12 ` [ruby-core:71897] [Ruby trunk - Feature #11781] [Open] Would it be possible to alias .prepend() towards .unshift() for class Array by default? shevegen
@ 2015-12-07 11:13 ` 6ftdan
  2015-12-09  3:56 ` [ruby-core:71969] " matz
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: 6ftdan @ 2015-12-07 11:13 UTC (permalink / raw
  To: ruby-core

Issue #11781 has been updated by Daniel P. Clark.


I believe you're coming from the Python language?

Ranges in Ruby use two dots 0..1.  The Array example you gave works, but the String example replaces the first character.

~~~ruby
abc = ['world!']
abc[0,0] = "Hello "
abc
# => ["Hello ", "world!"]

def = 'world!'
def[0..0] = 'Hello '
def
# => "Hello orld!"
~~~

`prepend` is already a keyword in the Ruby language for prepending a module in the ancestry chain: http://ruby-doc.org/core-2.2.3/Module.html#method-i-prepend

Personally I'm all for having Strings act more like an Array, but Strings already have a method on them that converts them to a character Array `String#char`.

If you'd like to discuss with me a gem for making Strings work with Array methods I've got a gem I need to update.  You can reach me on twitter: @6ftdan

----------------------------------------
Feature #11781: Would it be possible to alias .prepend() towards .unshift() for class Array by default?
https://bugs.ruby-lang.org/issues/11781#change-55304

* 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/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [ruby-core:71969] [Ruby trunk - Feature #11781] Would it be possible to alias .prepend() towards .unshift() for class Array by default?
       [not found] <redmine.issue-11781.20151207101222@ruby-lang.org>
  2015-12-07 10:12 ` [ruby-core:71897] [Ruby trunk - Feature #11781] [Open] Would it be possible to alias .prepend() towards .unshift() for class Array by default? shevegen
  2015-12-07 11:13 ` [ruby-core:71901] [Ruby trunk - Feature #11781] " 6ftdan
@ 2015-12-09  3:56 ` matz
  2015-12-09  7:28   ` [ruby-core:71988] " Eric Wong
  2015-12-09  7:51 ` [ruby-core:71989] " nobu
                   ` (2 subsequent siblings)
  5 siblings, 1 reply; 7+ messages in thread
From: matz @ 2015-12-09  3:56 UTC (permalink / raw
  To: ruby-core

Issue #11781 has been updated by Yukihiro Matsumoto.


It sounds nice. Since it is right before 2.3 release, it may be too late for it.

Matz.


----------------------------------------
Feature #11781: Would it be possible to alias .prepend() towards .unshift() for class Array by default?
https://bugs.ruby-lang.org/issues/11781#change-55378

* 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/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [ruby-core:71988] Re: [Ruby trunk - Feature #11781] Would it be possible to alias .prepend() towards .unshift() for class Array by default?
  2015-12-09  3:56 ` [ruby-core:71969] " matz
@ 2015-12-09  7:28   ` Eric Wong
  0 siblings, 0 replies; 7+ messages in thread
From: Eric Wong @ 2015-12-09  7:28 UTC (permalink / raw
  To: ruby-core

I prefer we avoid introducing needless aliases.

It increases human cognitive overhead for reviewing/auditing
code and also wastes machine memory + CPU cycles at startup.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [ruby-core:71989] [Ruby trunk - Feature #11781] Would it be possible to alias .prepend() towards .unshift() for class Array by default?
       [not found] <redmine.issue-11781.20151207101222@ruby-lang.org>
                   ` (2 preceding siblings ...)
  2015-12-09  3:56 ` [ruby-core:71969] " matz
@ 2015-12-09  7:51 ` nobu
  2015-12-09 12:34 ` [ruby-core:71995] " sawadatsuyoshi
  2015-12-09 12:46 ` [ruby-core:71996] " matthew
  5 siblings, 0 replies; 7+ messages in thread
From: nobu @ 2015-12-09  7:51 UTC (permalink / raw
  To: ruby-core

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 <i>str</i>.
  *
  *     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/

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [ruby-core:71995] [Ruby trunk - Feature #11781] Would it be possible to alias .prepend() towards .unshift() for class Array by default?
       [not found] <redmine.issue-11781.20151207101222@ruby-lang.org>
                   ` (3 preceding siblings ...)
  2015-12-09  7:51 ` [ruby-core:71989] " nobu
@ 2015-12-09 12:34 ` sawadatsuyoshi
  2015-12-09 12:46 ` [ruby-core:71996] " matthew
  5 siblings, 0 replies; 7+ messages in thread
From: sawadatsuyoshi @ 2015-12-09 12:34 UTC (permalink / raw
  To: ruby-core

Issue #11781 has been updated by Tsuyoshi Sawada.


`String#prepend` and `String#concat` are a pair that work similarly (with just the difference on where the new substring is inserted). And since there is already `Array#concat`, introducing `Array#prepend` would suggest that they should work similarly. However, while `Array#concat` takes an array and uses that as sub-array of the original array, the proposed `Array#prepend` takes an element and uses that as an element of the original array. This is higly confusing. I don't think the proposal is good.

----------------------------------------
Feature #11781: Would it be possible to alias .prepend() towards .unshift() for class Array by default?
https://bugs.ruby-lang.org/issues/11781#change-55406

* 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/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [ruby-core:71996] [Ruby trunk - Feature #11781] Would it be possible to alias .prepend() towards .unshift() for class Array by default?
       [not found] <redmine.issue-11781.20151207101222@ruby-lang.org>
                   ` (4 preceding siblings ...)
  2015-12-09 12:34 ` [ruby-core:71995] " sawadatsuyoshi
@ 2015-12-09 12:46 ` matthew
  5 siblings, 0 replies; 7+ messages in thread
From: matthew @ 2015-12-09 12:46 UTC (permalink / raw
  To: ruby-core

Issue #11781 has been updated by Matthew Draper.


I too would expect `Array#prepend` to be the opposite of `Array#concat`, taking `other_ary` as a parameter, because that seems to more closely match the relationship between `String#prepend` and `String#concat`.

It sounds potentially useful in its own right.

----------------------------------------
Feature #11781: Would it be possible to alias .prepend() towards .unshift() for class Array by default?
https://bugs.ruby-lang.org/issues/11781#change-55407

* 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/

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2015-12-09 12:15 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <redmine.issue-11781.20151207101222@ruby-lang.org>
2015-12-07 10:12 ` [ruby-core:71897] [Ruby trunk - Feature #11781] [Open] Would it be possible to alias .prepend() towards .unshift() for class Array by default? shevegen
2015-12-07 11:13 ` [ruby-core:71901] [Ruby trunk - Feature #11781] " 6ftdan
2015-12-09  3:56 ` [ruby-core:71969] " matz
2015-12-09  7:28   ` [ruby-core:71988] " Eric Wong
2015-12-09  7:51 ` [ruby-core:71989] " nobu
2015-12-09 12:34 ` [ruby-core:71995] " sawadatsuyoshi
2015-12-09 12:46 ` [ruby-core:71996] " matthew

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).