Nipun Tanay Nipun Tanay - 5 months ago 13
Ruby Question

create an array having size or length 10 and each element increment by 2. Consider 1st element as 0.

"How to set size of an array at the time of its creation. Using the same, create an array having size or length 10 and each element increment by 2. Consider 1st element as 0."

I am new to ruby . Is there a single or two line command for this ? I don't want to add all the 10 elements separately.

Answer

Try this, not sure this is the exact answer you are looking for

Array.new(10){ |index| index * 2 }
Comments