upcase

Hi!

Can anyone tell me why the first values are also upcased??

a=['a','b','c']

a=a.zip a.dup

a.map{|x,y| y.upcase!}

a

thanks Opti

Shallow copy:

irb(main):001:0> a = ['a', 'b', 'c']
=> ["a", "b", "c"]
irb(main):002:0> b = a.dup
=> ["a", "b", "c"]
irb(main):003:0> a[0].object_id
=> 360
irb(main):004:0> b[0].object_id
=> 360
irb(main):005:0> a[1].object_id
=> 380
irb(main):006:0> b[1].object_id
=> 380
irb(main):007:0> a[2].object_id
=> 400
irb(main):008:0> b[2].object_id
=> 400

Since you are doing an in-place upcase you are affecting both arrays
because both arrays are storing the same objects.

···

On Thu, Aug 26, 2021 at 9:57 AM Die Optimisten <inform@die-optimisten.net> wrote:

Hi!

Can anyone tell me why the first values are also upcased??

a=['a','b','c']

a=a.zip a.dup

a.map{|x,y| y.upcase!}

a

thanks Opti

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<ruby-talk list: member options login page>

Wow. That's a head-scratcher. What's happening here is that map is only being asked to return the upcased value. Unlike each, map will return a new array at the end of the enumeration.

This works (using assignment and a deliberate return of the input)

a=['a','b','c']

a=a.zip a.dup

a = a.map{|x,y| [x, y.upcase] }

a

Walter

···

On Aug 26, 2021, at 9:57 AM, Die Optimisten <inform@die-optimisten.net> wrote:

Hi!

Can anyone tell me why the first values are also upcased??

a=['a','b','c']

a=a.zip a.dup

a.map{|x,y| y.upcase!}

a

thanks Opti

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<ruby-talk list: member options login page>

Hi,
thank you!
Therefore I used .dup ??
Is there a method for a deep(er) copy?
thanks Opti

···

Am 26.08.21 um 16:15 schrieb Eric Anderson:

Shallow copy:

irb(main):001:0> a = ['a', 'b', 'c']
=> ["a", "b", "c"]

The problem is in the use of "zip" method.

···

On Thu, Aug 26, 2021 at 11:35 AM Die Optimisten <inform@die-optimisten.net> wrote:

Hi,
thank you!
Therefore I used .dup ??
Is there a method for a deep(er) copy?
thanks Opti

Am 26.08.21 um 16:15 schrieb Eric Anderson:
> Shallow copy:
>
> irb(main):001:0> a = ['a', 'b', 'c']
> => ["a", "b", "c"]

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<ruby-talk list: member options login page>

Disregard my message. I didn't understand the issue. Sorry.

···

On Thu, Aug 26, 2021 at 11:38 AM Clavius Tales <clavius.tales@gmail.com> wrote:

The problem is in the use of "zip" method.

On Thu, Aug 26, 2021 at 11:35 AM Die Optimisten <inform@die-optimisten.net> > wrote:

Hi,
thank you!
Therefore I used .dup ??
Is there a method for a deep(er) copy?
thanks Opti

Am 26.08.21 um 16:15 schrieb Eric Anderson:
> Shallow copy:
>
> irb(main):001:0> a = ['a', 'b', 'c']
> => ["a", "b", "c"]

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<ruby-talk list: member options login page>

a = %w[a b c]
a = a.zip a.map(&:dup)
a.map { |_x, y| y.upcase! }
a

···

On Thu, Aug 26, 2021 at 11:35 AM Die Optimisten <inform@die-optimisten.net> wrote:

Hi,
thank you!
Therefore I used .dup ??
Is there a method for a deep(er) copy?
thanks Opti

Am 26.08.21 um 16:15 schrieb Eric Anderson:
> Shallow copy:
>
> irb(main):001:0> a = ['a', 'b', 'c']
> => ["a", "b", "c"]

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<ruby-talk list: member options login page>

Anyone wants to write a recursive .dupr ?
setting the depth as argument -> arr.dupr(2) # max_depth=2 (default=0
means all)
Opti

-> should be integrated in Ruby!
Thanks Opti

···

Am 26.08.21 um 16:38 schrieb Clavius Tales:

The problem is in the use of "zip" method.

On Thu, Aug 26, 2021 at 11:35 AM Die Optimisten > <inform@die-optimisten.net <mailto:inform@die-optimisten.net>> wrote:

    Hi,
    thank you!
    Therefore I used .dup ??
    Is there a method for a deep(er) copy?

Depending on how complex your object is, this may be sufficient to deep
copy an object:

copy = Marshal.load(Marshal.dump(a))

I would probably just use map with the non-mutating version of upcase to
both copy and transform:

a = ['a', 'b', 'c']
a = a.zip a.map(&:upcase)

Eric

···

On Thu, Aug 26, 2021 at 10:35 AM Die Optimisten <inform@die-optimisten.net> wrote:

Hi,
thank you!
Therefore I used .dup ??
Is there a method for a deep(er) copy?
thanks Opti

Am 26.08.21 um 16:15 schrieb Eric Anderson:
> Shallow copy:
>
> irb(main):001:0> a = ['a', 'b', 'c']
> => ["a", "b", "c"]

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<ruby-talk list: member options login page>