1

I'm getting an unexpected return value with this code:

str = ''; 'abc'.chars.map {|c| str<<c}

Expected output:

["a", "ab", "abc"]

Actual output:

["abc", "abc", "abc"]

Adding a puts(str) for debugging:

str = ''; 'abc'.chars.map {|c| puts(str); str<<c}

a
ab
=> ["abc", "abc", "abc"]

Why is the above code not returning the expected output? Thanks.

builder-7000
  • 7,131
  • 3
  • 19
  • 43
  • You can avoid this confusion by using the `# frozen_string_literal: true` directive. It will prevent you from using destructive methods like `String#<<`. There is a performance penalty in some scenarios, but the tradeoff is worth it for most teams. – Jared Beck Jan 21 '22 at 05:23

2 Answers2

5

From the fine manual:

string << object → string.
Concatenates object to self and returns self

So str << c in the block alters str and then the block itself evaluates to str.

You could say this to get the results you're after:

str = ''
'abc'.chars.map { |c| (str << c).dup }
mu is too short
  • 426,620
  • 70
  • 833
  • 800
  • 1
    Awesome, thanks a lot. Now i know a bit more about ruby blocks. So Object#oid was the same, so the block returned `str` multiple times. I needed `dup` to create a different object on each iteration. – builder-7000 Jan 21 '22 at 05:22
  • 1
    @builder-7000 you don't have to use `dup` necessarily, you could also create new objects by re-assigning `str`, e.g. `'abc'.chars.map { |c| str = "#{str}#{c}" }` – Stefan Jan 21 '22 at 09:30
2

It's because each element in your map is returning a reference to str's value, not the actual value of str. So your map is returning 3 references to the value of str and not the value of str at the time of iteration; because each reference to str points to one place in memory at the end that has 'abc', that's why you see the final value of str 3 times.

Allison
  • 1,925
  • 1
  • 18
  • 25
  • You might find some of the answers to this question helpful: https://stackoverflow.com/questions/1872110/is-ruby-pass-by-reference-or-by-value – Allison Jan 21 '22 at 05:26