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.

No comments:

Post a Comment