Ruby is a new and quickly growing object orientated programming language. For how easy Ruby is to learn, it is incredibly powerful. If you’re a quick learner it is possible to learn the majority of Ruby within a week.
Before starting make sure you have the latest version of Ruby installed and some form of editor or IDE installed such as Notepad or SciTE IDE. You will need these handy as these tutorials use a ‘try-it-out’ approach to coding.
The ‘Hello World’ App
#My "Hello World" app puts "Hello World"
So what exactly is all this, you say. Let’s go through it line by line:
The first line is a comment. In Ruby all comments start with “#”. Unfortunately in Ruby there is no such thing as block comments and so each comment must be a single line comment beginning with the “#” character. Comments may include any character as long as they are after the “#”. They are totally optional.
The second line is the actual output. “puts” tells the console/computer to output the following. In this case the following is a string “Hello World”. A string is simply a sentence that can include any character as long as they are inside the quotation marks. These quotation marks are important otherwise “puts” would think it is outputting a variable (more on variables later).
(Extra: “print” will do a similar job to “puts” – but not the same.)
Try outputting different strings and when you’re done, continue on.
Ruby is dynamically typed. This means that you do not need to associate a variable with a certain type. You can just declare the variable and the value and it is set.
#Different types of variables var1 = "Hello World" var2 = "5" var3 = 5 var4 = 3.14 var5 = true var6 = "false"
The rule for variables is:
variable_name = value
The variable name can consist of letters, numbers and some symbols such as the underscore “_”. The only thing, at this level, you must remember is that variables start with a lowercase letter.
• If the value of a variable has quotation marks, such as var1, var2 and var6 it is a string.
• If the value of a variable has a whole number then it is an integer, for example var3.
• If the value of a variable has a decimal it is a float, for example var4.
• If the value of a variable is either true or false and not in quotation marks, then it is a boolean, such as var5 but not var6.
Now although Ruby is not statically typed you must be careful when referring to variables of what type they are. You can’t mix types in Ruby without using the proper methods. For example, have a look at the code below and think to yourself what it will output.
var_1 = "5" var_2 = 5 puts var_1 + var_2
So, I’m going to guess you thought it was10? Unfortunately you’re wrong to a certain extent. The above code will throw an error out at us at line 4 – in programming you count empty lines.
Why is this you say? Well it is because you are trying to add a string with a number. They are two different things and don’t go well together!
To fix this type of problem there are three methods built in to Ruby we can use:
to_s to_i to_f
These are all methods (we’ll come on to methods later). Methods are attached to a variable name using a period (.) or more commonly known as a full-stop. So, can you have a guess at what the methods above will do? On the next page are the relative meanings for each, but in English terms that you can understand.
• To String
• To Integer
• To Float
Below is the code that will solve our problem.
var_1 = "5" var_2 = 5 puts var_1.to_i + var_2 puts var_1 + var_2.to_s
The above code will now generate the two correct answers that our previous piece of code was having trouble working out which one we really wanted.
The output is:
The computer took, in line 4, our var_1 and converted it into an integer so to make it a math sum. In line 6 it converted var_2 into a string so it simply adds the strings together.
If you want to experiment using some variables, mixing them up and then outputting them – now is your chance.
A Tiny Bit More On Methods
Now before we move onto input I just want to talk a bit on some other common built in methods. Methods are simply put, processes. They attach to a variable by a period and it is possible to have a method on a method.
As I stated before, there are the three type conversion methods. Below is a short list of some other commonly used methods and what they do:
upcase = Converts every letter of a string to its uppercase form.
downcase = Converts every letter of a string to its lowercase form.
capitalize = Converts just the first letter of a string to a capital.
Try experimenting with these methods and when you’re done, move on to input.
Getting input from the user and keyboard is simple and I will explain it below. Here is some code that accepts user input.
#gets input from keyboard STDOUT.flush var1 = gets.chomp puts var1
Now I hope this code isn’t confusing you as it honestly isn’t that difficult once you take a look line by line.
Line 1 is a comment, you’ve learnt about these before so I don’t need to explain this.
Line 3 is not needed although if you start to get problems whereby your computer isn’t accepting your input put this line in.
Line 4 is the main part to get input. You first declare a variable, in this case var1, which will store the user’s input. Next is “gets”. This does what it says and gets input from the user.
However, without the “chomp” method, when “puts” outputs var1, it will output whatever you input with a newline sign (“\n”). For this example I’ve inputted “ruby”:
Now I’m sure you don’t want that ugly newline sign there and this is why we add the “chomp” method to our “gets”. It now outputs:
However just outputting what the user inputted isn’t interesting. Maybe if we added it in a string it would be?
var1 = gets.chomp puts "You inputted, " +var1 puts "You just love " +var1+ "!"
As you can see, you use “+” signs along with the variable name when you want to output a variable within a string. The above produces:
You just love ruby!
And with that it brings us to our first Summary…
Summary Of Part 1
We’ve currently learnt about comments, variables, basic methods and keyboard input. So to complete this first section I’d like you to write a script that says asks for the person’s name and age and then outputs it in a good English sentence with capital letters.
(Extra: If you feel this is too easy, why not add their surname along with how old they will be in 5 years time.)
If you don’t have time to do that, or you’ve finished you can check you code against mine and compare. I haven’t included the ‘extra’ code in this sample:
#Summary 1 puts "Hello there, what's your name?" theirName = gets.chomp puts "What's your age?" theirAge = gets.chomp puts "Well, " +theirName.capitalize+ ", it's nice to meet you and you're very young at just " +theirAge+ "years old!"
Did You Know: In Ruby you don’t need to end a line with a semi-colon “;” as you do in C, C++ and Java (if you don’t know what they are, they are other programming languages). You will learn slightly more about this in the next section.
I will be writting part 2 and onwards in the near future.