Project

General

Profile

Actions

Bug #14641

closed

Shouldn't Array#map not modify its array?

Added by hartator (Julien Khaleghy) over 6 years ago. Updated over 6 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
2.5.0 and 2.4.2
[ruby-core:86372]

Description

Hi,

Thanks for the awesome work you guys have been doing on Ruby for all this years.

I might be missing something obvious, but I thought Array#map doesn't modify the array it's called on?

Ie.

ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-linux]
    a = ['a','b'] 
..    
=> ["a", "b"]
    a.map{|e| e << 'c'} 
=> ["ac", "bc"]
    a.map{|e| e << 'c'} 
=> ["acc", "bcc"]
    a.map{|e| e << 'c'} 
=> ["accc", "bccc"]
    a.map{|e| e << 'c'} 
=> ["acccc", "bcccc"]
   a
=> ["acccc", "bcccc"]

Expected a to be still ["a", "b"].

Same in Ruby 2.4 ruby 2.4.2p198 (2017-09-14 revision 59899) [x86_64-darwin16]

irb(main):003:0*  a = ['a','b']
=> ["a", "b"]
irb(main):004:0>  a.map{|e| e << 'c'} 
=> ["ac", "bc"]
irb(main):005:0>  a.map{|e| e << 'c'} 
=> ["acc", "bcc"]
irb(main):006:0>  a.map{|e| e << 'c'} 
=> ["accc", "bccc"]
irb(main):007:0>  a.map{|e| e << 'c'} 
=> ["acccc", "bcccc"]
irb(main):008:0>  a 
=> ["acccc", "bcccc"]

Thanks for you guys time.

Actions

Also available in: Atom PDF

Like0
Like0Like0