0

Let's say I have a number 5

How would I convert this to an array [1, 2, 3, 4, 5]?

The application here is I have an instance variable @pages and I want to create a pagination view.

2 Answers 2

3
pages = 5
array_of_numbers = (1..pages).to_a

(1..pages) will give you a Range object and to_a will convert it into an array.

Sign up to request clarification or add additional context in comments.

Comments

0

https://apidock.com/ruby/Integer/times pointed me to a solution

pages = 5 (1..pages).each { |n| puts n }

2 Comments

You have a lot of other ways in older answers. I guess isn't necessary to create a new question for that.
This won't return an array. It will just print the nos.

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.