Monday, 3 March 2014

How To Use The If conditional In Ruby

How do most computer languages evaluate and make simple decisions? Mostly through the use of the conditional 'If' statement. Whats more Ruby is no different though a lot might argue including myself a hell of a lot easier. So lets take a look at this statement in action shall we?

irb(main):001:0> age = 50
=> 50
irb(main):002:0> if age < 50
irb(main):003:1> puts('still a few years on the clock yet')
irb(main):004:1> else
irb(main):005:1* puts('too bad')
irb(main):006:1> end
too bad

First off we set assigned 50 to the age. Then we started with the conditional 'if' to evaluate the age where we used an operator < which means less than. So we said if age was less than 50 print the first message or if not print the second message.

And that's a simple way to use the conditional 'if' operator in Ruby.

How To Assign Basic Values To Variable In Ruby

Assigning Values To Variables in Ruby is a pretty easy thing to do. Once you learn it there's no end to the fun you can have with this programming language.

Here are a couple of examples as how you can manipulate variables and their values in Ruby.

irb(main):001:0> x = 50
=> 50
irb(main):002:0> y = 100
=> 100
irb(main):003:0> puts x + y

Firstly we assigned numbers to the variables x and y. Then we used the 'puts' method to output the combined value of x + y to arrive at our total of 150! Simples!

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.

Monday, 27 January 2014

Learn Ruby Programming The Easy Way

The Ruby Programming languages is probably one of the easiest and most fun of all programming languages to learn. It has been around now for a good 20 years or so and has of recent gained tremendous popularity through Ruby on rails its web application framework.

For new learners of Ruby one of the things that stands oiut the most is its plain English syntax. It's simply a very easy language to understand as well as being very powerful.

Okay lets dive into an example or two using this scripting language. First off we have a simple hello world program. This is the standard way to kick off a tutorial in the programming world.

Firstly, you'll need to install interactive Ruby which can be invoked at the command line via irb. This will allow you to write simple lines of code and see them in action.

irb(main):001:0> puts('hello world' * 5)
hello worldhello worldhello worldhello worldhello world

Here I've used the * multiplication operator to times the output by 5, hence hello world is written 5 times!
irb(main):001:0> array = ['hello',1,'you',8]
=> ["hello", 1, "you", 8]
irb(main):002:0> puts array.length
=> nil
Here we have a standard array that contains words and numbers. And underneath it we have used the 'length' feature to return its length = 4.

Those are just a few examples of the Ruby language in action. To learn more the following self teach book is an invaluable source for any learner of the Ruby programming language.

Thursday, 23 January 2014

Learning Python For Beginners

 Python is a very powerful & programming language. It is used for a variety of application one of the most notable users of the Python programming language is NASA. Python  is an easy language to learn when you compare it to other languages like C and C++. This makes it a very popular choice especially amongst people who have never programmed before.It only takes a few minutes to write python code and you'd be surprised how fast you can pick it up.

Some python code examples written in IDLE python's interactive shell.

>>> print('Hello out there big wide world')
Hello out there big wide world

At the Python interpreter we have used the "print" command to simply print out some data "Hello out there big wide world" Easy!!!

Now lets use python as a calculator:

>>> print(2 + 2)

Something a little different here. Can you guess?

 >>> print('Hello new python learner ' * 2)
Hello new python learner Hello new python learner

We first print the string: 'Hello new Python learner' and we instruct our program to multiply it twice, * 2. This multiplication operator * then prints our line twice!

Python is not only a fun programming language to learn - but as previously stated it is an easy entry point for beginners wishing to become proficient in computer programming. Once more once you've learned the basics of the Python language learning any other language after that will be a snap!