I\'m new for ruby. Trying to get all numbers in an array with an method.
x = [1..10]
expected result.
=> [1,2,3,4,5,6,7,
When you type [1..10]
, what you actually have is an Array containing a single Range object. If you want an array of FixNums, you actually drop the []
s and call to_a
on the range itself:
irb(main):006:0> x = (1..10).to_a
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
you want to display it?
# This dumps the object to console
x = (1..10).to_a
puts x.inspect
# This prints items individually...
x = (1..10).to_a
x.each do |num|
puts num
end
# This prints only numbers....
x = (1..10).to_a
x.each do |num|
if num.is_a?(Integer)
puts num
end
end