On 5 July 2014 17:52, Arup Rakshit <aruprakshit / rocketmail.com> wrote:

> I did use the below  :-
>
> "12\^ abc23 44".scan(/\d+/).map(&:to_i)
> # => [12, 23, 44]
>
> Now, I thought, let's try to do this without creating an intermediate
> array.
>
> Then I saw, String#scan supports block, so I can write
>
> ar = []
> 
> "12\^ abc23 44".scan(/\d+/) { |e| ar << e.to_i }
>
> Then I thought, lets use the *block support* of `#scan` some other way and
> I
> came up with the below :-
>
> "12\^ abc23 44".enum_for(:scan, /\d+/).with_object([]) { |e, a| a <<
> e.to_i }
> # => [12, 23, 44]
>
> It works, but not I am able to understand, if it is creating the
> intermediate
> array ( as #scan do without block), or block version of #scan is getting
> called.
>


Does it matter that much? BTW if all you care about is only creating a
single array, you could use #map!


-- 
  Matthew Kerwin
  http://matthew.kerwin.net.au/