Trying to do something like:
def action(number)
nums = [1...number]
code_here...
end
Do I need to write up a "count" snippet for this to work? Or is something similar to what I'm trying to do possible?
Trying to do something like:
def action(number)
nums = [1...number]
code_here...
end
Do I need to write up a "count" snippet for this to work? Or is something similar to what I'm trying to do possible?
There's another option to use enumerators:
1.upto(10).to_a #=> [1,2,3,4,5,6,7,8,9,10]
There are quite a few useful ones. For example, if you only need every other number up to certain number you can use:
0.step(100, 2).to_a #=> [0,2,4,6,8,..., 98,100]
Another option would be to use Array constructor:
Array.new(10) { |i| i + 1 } #=> [1,2,3,4,5,6,7,8,9,10]
which is a bit more verbose, but gives you much more freedom:
Array.new(10) { |i| 2 ** i } #=> [1,2,4,8,16,32,64,256,512]
(1...).first(10)?Enumerator.produce(1,&:succ) (less than 2.7 Enumerator.new {|y| i = 0; loop {y << i+=1}}).first(10).1.step.take(10) is another optionThere are two solutions for this as following:
If you want to add number in the array than:
nums = [*1..number]
# or
nums = (1..number).to_a
Ignoring last number than:
nums = [*1...number]
# or
nums = (1...number).to_a
1.upto(number) or Array.new(number) {|i| i + 1} or to start at 0 number.times or Array.new(number).each_index etc.