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/