Hi, I'm getting unexpected output with this code:
str = ''; 'abc'.chars.map {|c| str<<c}
Expected output:
["a", "ab", "abc"]
Actual output:
["abc", "abc", "abc"]
Adding `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? Ruby version is 3.0.3p157.
Thanks.
They're the same string. You likely want to either use Enumerable#reduce or
Enumerator#produce instead, or this variant:
s = "abc"
# => "abc"
s.size.times.map { |i| s[0..i] }
# => ["a", "ab", "abc"]
···
On Thu, Jan 20, 2022 at 9:05 PM Sergio Romero <yo@sergioro.com> wrote:
Hi, I'm getting unexpected output with this code:
str = ''; 'abc'.chars.map {|c| str<<c}
Expected output:
["a", "ab", "abc"]
Actual output:
["abc", "abc", "abc"]
Adding `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? Ruby version is
3.0.3p157.
Thanks.
Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Awesome, thanks a lot. So `Object#oid` was the same, so the block returned the same`str` multiple times. Also found this other solution with `Object#dup`:
'abc'.chars.map { |c| (str<<c).dup }
···
On 01/21/2022 12:11 AM Brandon Weaver <keystonelemur@gmail.com> wrote:
They're the same string. You likely want to either use Enumerable#reduce or Enumerator#produce instead, or this variant:
s = "abc"
# => "abc"
s.size.times.map { |i| s[0..i] }
# => ["a", "ab", "abc"]
On Thu, Jan 20, 2022 at 9:05 PM Sergio Romero <yo@sergioro.com mailto:yo@sergioro.com > wrote:
> Hi, I'm getting unexpected output with this code:
>
> str = ''; 'abc'.chars.map {|c| str<<c}
>
> Expected output:
>
> ["a", "ab", "abc"]
>
> Actual output:
>
> ["abc", "abc", "abc"]
>
> Adding `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? Ruby version is 3.0.3p157.
> Thanks.
>
> Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org ?subject=unsubscribe>
> <http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
>
Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Yep. Same thing happens when someone does Hash.new() versus Hash.new {
···
h, k| h[k] = }
On Thu, Jan 20, 2022 at 9:28 PM Sergio Romero <yo@sergioro.com> wrote:
Awesome, thanks a lot. So `Object#oid` was the same, so the block
returned the same`str` multiple times. Also found this other solution with
`Object#dup`:
'abc'.chars.map { |c| (str<<c).dup }
On 01/21/2022 12:11 AM Brandon Weaver <keystonelemur@gmail.com> wrote:
They're the same string. You likely want to either use Enumerable#reduce
or Enumerator#produce instead, or this variant:
s = "abc"
# => "abc"
s.size.times.map { |i| s[0..i] }
# => ["a", "ab", "abc"]
On Thu, Jan 20, 2022 at 9:05 PM Sergio Romero <yo@sergioro.com> wrote:
Hi, I'm getting unexpected output with this code:
str = ''; 'abc'.chars.map {|c| str<<c}
Expected output:
["a", "ab", "abc"]
Actual output:
["abc", "abc", "abc"]
Adding `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? Ruby version is
3.0.3p157.
Thanks.
Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>