Array Building idiom

  • Thread starter John-Mason P. Shackelford
  • Start date
J

John-Mason P. Shackelford

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
(e-mail address removed)
http://pearsonedmeasurement.com
 
M

Martin DeMello

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
 

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