Friday, 14 February 2014

Basic Variables In Ruby

Basic Variables

Variables are very important in Ruby programming, but the good news is that you don;t have to be a rocket scientist to be able to understand them because they're really quite straightforward. For our first example we'll take a look at assigning a letter to a number.

irb(main):001:0> x = 20
=> 20

Here we have assigned the letter x to equal or represent 20. Now x = 20 and we can prove this by asking Ruby to print out the value of x.

irb(main):002:0> puts x
=> nil

You can now play around  with your variables by adding or multiplying with them like so.
irb(main):003:0> x + 5
=> 25

We can see here that the value of x is now 25 because we've added a + sign with a 5.

 Later on we'll take a closer look at these operators * + - etcwhen we delve more into Ruby variables.

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.

How To Add Elements To A Ruby Array Using (.push)

In the last post we examined how to delete elements from the Ruby array using the .pop method. In this short code snippet we're going to take a look at how to use Ruby to add extra elements to the array via the .push method. As always head on over to your shell if you're using Linux and go into irb. Then type out the following code.

new_array = ['Britain','Thailand','USA']

=> ["Britain", "Thailand", "USA"]

And there you have our array of countries like so. Next type in this.


Here we see the .push method along with the latest country we want to add to our Ruby array list.

=> ["Britain", "Thailand", "USA", "Italy"]

Once you hit enter your newly updated array is returned with an addition to it. It's worth mentioning here that we can also push elements into the array via this method <<.

irb(main):003:0> new_array << 'germany'

And the returned output is:

=> ["Britain", "Thailand", "USA", "Italy", "germany"]

Germany being added to our new Ruby array.

Saturday, 8 February 2014

Reversing The Elements In A Ruby Array

So you've got your Ruby array and you want to reverse the elements in it! With the Ruby programming this task is easily accomplished with the following code. First off go and create an array in irb something like this.

my_array = ['mark','dave',1,2,3,'x']

Next we will use the reverse command on this array like so:

puts my_array.reverse

This will print the following output to your screen.


Notice here that this output has been printed out like a list. The reason for this is that we used 'puts' to output it. If you want to see the output of your array printed on the same line, simply substitute the 'puts' command for the Ruby 'print' command like so.

print my_array.reverse

["x", 3, 2, 1, "dave", "mark"]

And hey voila Ruby will print the output on the same line!

Tuesday, 4 February 2014

How To Add Extra Elements Into A Ruby Array

Adding Additional Things Into A Ruby Array

Once you've got your array in Ruby you might want to add extra elements to it. With ruby this is easy.

Create a new array, this time I've created an array of numbers. The keener eyed reader out there will have spotted that you don't need quotations around the numbers - but they do have to be divided by commas. So go ahead and create a new array:

 numbers = [1,2,3,4,5]

Now we simple want to add extra elements onto the end of this array. We accomplish this by a line of code like this:


We then hit enter and here is the output:

[1, 2, 3, 4, 5, "mark"]

There you can see 'Mark' has been added onto the end of the array.


 [1, 2, 3, 4, 5, "mark", "dave"]

And finally we're able to add more and more elements as we want. And that's how to add elements onto an array in Ruby.

Monday, 3 February 2014

How To Get The Length Of An Array In Ruby

When creating an array in Ruby one thing you might want to do is to get the length of that array. In most other programming languages this can be a rather drawn out affair. However, this is not the case with ruby. Getting the length of an array is a snap. First enter into irb at the command line and set up your array as shown below:

 my_array = ['element1','element2','element3']

Now you get the length of the array by simply by adding the 'length' operator onto your array.

puts my_array.length

Now hit enter to get the result:


And that's all there is to getting the length of an array in Ruby.

Sunday, 2 February 2014

How To Iterate Through A Ruby Array

Okay in our last post we talked about creating a simple array using the ruby programming language. In this next little bite sized code example I'm going to show you how to iterate through each element of the array.

First off go ahead and fire up irb, Ruby's interactive environment. In Linux go ahead and open a shell and type in irb, if you're in Windows open the dos prompt and type in irb.

Go ahead and create the array.

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

Then type in the following:

friends.each{|friends| puts friends}

Hit enter and the above code iterate through the array and print out its contents like so...


However, if you want the contents of your Ruby array to be printed out in a line rather than list style use the 'print' command rather than 'puts'.

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.

=> "dave"

Element 0 in the array returns 'Dave'

=> "pete"

Element 1 returns 'Pete'

=> "paul"

Finally element 2 returns 'paul'

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