Wednesday 12 February 2014

Creating An Array Of Numbers In Ruby


Our next bite sized chunk of Ruby coding will focus on how to create a simple array of numbers. Okay, head on over to your favourite terminal, fire it up and input the following.

irb(main):004:0> numbers = Array(0..9)

Here we've set the numbers in our array from zero to nine. Then we hit enter and the output is this.

 => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Next, enter the following code:


 print(" #{numbers} ")

And hey voila Ruby out puts  your array of numbers.


[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] => nil

And that's your basic Ruby array of numbers.










No comments:

Post a Comment