Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   Array Building idiom (http://www.velocityreviews.com/forums/t832310-array-building-idiom.html)

John-Mason P. Shackelford 08-10-2006 07:23 PM

Array Building idiom
 
What other idioms do you use for:

a=[]; 10.times{a<<callme() }
Array.new(10).map{ callme() }

Note: callme() returns a new value with each call.

Do I understand correctly that the former will be illegal under 1.9.1?
--
John-Mason Shackelford

Software Developer
Pearson Educational Measurement

2510 North Dodge St.
Iowa City, IA 52245
ph. 319-354-9200x6214
john-mason.shackelford@pearson.com
http://pearsonedmeasurement.com


Martin DeMello 08-10-2006 07:46 PM

Re: Array Building idiom
 
On 8/11/06, John-Mason P. Shackelford <jpshack@gmail.com> wrote:
> What other idioms do you use for:
>
> a=[]; 10.times{a<<callme() }
> Array.new(10).map{ callme() }
>
> Note: callme() returns a new value with each call.
>
> Do I understand correctly that the former will be illegal under 1.9.1?


Illegal on what basis? Also, Array.new takes a block while
constructing the array, so you needn't use map:

irb> Array.new(10, rand(100))
=> [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

irb> Array.new(10) { rand(100) }
=> [74, 59, 97, 59, 65, 12, 64, 16, 2, 59]

martin



All times are GMT. The time now is 07:08 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.