Saturday 1 February 2014

How To Create A simple Array Using The Ruby Programming Language



Making an array in Ruby is simple. First of all take a look at my example of an array below.  

friends = ['dave','pete','paul']

Here I've created an array called friends for this code example. You are free to create arrays in Ruby with what ever you want!

You'll see that this array has two elements in it! Yes, Two!!! You might be thinking this guy can't count right? Wrong! You see the first element of any array always starts from zero. So 'dave' = 0  'pete = 1' and 'paul = 2'.

Lets take this a step further and access each element of the array starting from the first element at zero.

 friends[0]
=> "dave"


Element 0 in the array returns 'Dave'

friends[1]
=> "pete"
 

Element 1 returns 'Pete'

friends[2]
=> "paul"




Finally element 2 returns 'paul'

And that is how you create a bsic array using the Ruby programming language.



No comments:

Post a Comment