About 1.8.7 Array#map! without a block



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!

.



Relevant Pages

  • Re: foreach enhancement
    ... > that all variables in the array literal have the same type. ... > If it is not present in the type we have to explicity state an increment ... >> rename it to isin) as you suggested so that it doesn't emit the generator ... With lists you end up with two different sets of values added, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: searching for missing element in an array
    ... What happens if the array was initialized to all -1 values? ... we know that a zero is stored in the slot of the missing number? ... initialization of the array. ... there are n-1 numbers present. ...
    (comp.programming)
  • Re: Simple optimization
    ... number in the array. ... might begin to see patterns in the simulation. ... generator once, make as many numbers as needed, then get rid of it. ... certain areas - the real world doesn't go in for normal distributions as ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: how to return mulitple corresponding values
    ... "Biff" wrote: ... It's not actually multiplying numbers. ... This will return an array like this: ... If you're not using dynamic named ranges then I would "dummy down" this ...
    (microsoft.public.excel.worksheet.functions)
  • Re: how to return mulitple corresponding values
    ... I know what the formula does but what do you mean by: sorted order i.e.the ... "Biff" wrote: ... It's not actually multiplying numbers. ... These logicals are multiplied together and result in an array of 1's ...
    (microsoft.public.excel.worksheet.functions)