1

I have an integer with a value

I want to create an array with nil values representing this integer

For example:

i = 5
# The result i want is
[nil, nil, nil, nil, nil]

What is the most Ruby way of doing this? Aka, the shortest way. Without need of looping et c.

1
  • It took you more time to write this question than it would have taken to check the Array documentation. Commented Mar 25, 2015 at 20:45

2 Answers 2

3

This is very basic question:

i = 5
Array.new(i) #=> [nil, nil, nil, nil, nil]
Sign up to request clarification or add additional context in comments.

1 Comment

Great, Thanks! Stackoverflow is for all kinds of questions :-) And since i couldn't find the answer for it here, I asked :-)
1
i = 5
[nil]*i #=> [nil, nil, nil, nil, nil]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.