user2071444
user2071444

Reputation:

Increment array till a variable's value using ruby array splat

I am using the ruby array splat like this:

array = *1,2,3 
Output = [1, 2, 3] 
count = 10 #Initializing count 

Question: I want the array to be continued till the count = 10 when I try this it doesn't work array = *1,..,count

Expected Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Is there any possible way to succeed with this way of approach.

Upvotes: 0

Views: 1079

Answers (5)

tkroman
tkroman

Reputation: 4798

(1..count).to_a, or just (1..count) if you need Enumerable object but not explicitly Array.

Upvotes: 1

Arup Rakshit
Arup Rakshit

Reputation: 118299

count = 10
count.times.map(&:next)
#=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Upvotes: 0

Stefan
Stefan

Reputation: 114248

You can simply use Kernel#Array:

Array(1..count)
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Upvotes: 2

Supersonic
Supersonic

Reputation: 430

You will have to do it this way:

array = [*1..count]

Upvotes: 0

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230531

count = 10
*(1..count) # => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Upvotes: 3

Related Questions