About 1.8.7 Array#map! without a block

  • Thread starter Eustaquio 'TaQ' Rangel
  • Start date
E

Eustaquio 'TaQ' Rangel

Hi there.

I was wondering how to use Array#map! with a block. On the usual way, we
have

irb(main):001:0> a = [1,2,3]
=> [1, 2, 3]
irb(main):002:0> a.map! {|i| i*2}
=> [2, 4, 6]

Now with 1.8.7 we have:

irb(main):003:0> e = a.map!
=> #<Enumerable::Enumerator:0xb7db015c>
irb(main):004:0> e.next
=> 2
irb(main):005:0> a
=> [#<Generator:0xb7d9b9dc @queue=[4],
@cont_endp=#<Continuation:0xb7d9b1a8>, @index=1,
@block=#<Proc:0xb7da7ffc@/usr/local/lib/ruby/1.8/generator.rb:71>,
@cont_yield=#<Continuation:0xb7d9aed8>, @cont_next=nil>, 4, 6]
irb(main):006:0> a.size
=> 3

I got an Enumerable::Enumerator with a.map! and then the first call to
e.next returned me the first "a" element, 2. Ok, two questions:

1 - Since I created the Enumerable::Enumerator with map!, I presume it's
used on some way to change the elements on "a", so what should I use
with e.next to change the value, say, multiplying by 2 as on the first
case and have a [4,8,12] array on the end?

2 - Why I get a Generator on "a" elements when I just call e.next? The
cause it's what I'm missing to use with next, as asked on the first
question?

Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top