Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
1  
Your question is not clear. What are 2 and 3 doing? –  sawa Jun 4 '13 at 11:19
    
Question Edited, I was just showing what I tried first. Basically I was expecting the output to be till count = 10. –  Mark V Jun 4 '13 at 11:25

5 Answers 5

up vote 2 down vote accepted

You will have to do it this way:

array = [*1..count]
share|improve this answer
count = 10
*(1..count) # => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
share|improve this answer

You can simply use Kernel#Array:

Array(1..count)
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
share|improve this answer
    
I Like Kernel#Hash and Kernel#Array. so +1. –  Arup Rakshit Jun 4 '13 at 11:34

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

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.