2. The basics

This is an optional chapter that will introduce a few basic concepts we will be using during the course. We take their knowledge for granted, this is why it is important to get familiar with them before the workshop session.

What's a programming language?

Much like a regular language, a programming language is a set of rules, vocabulary and grammar. Common programming languages use English words or constructs to express computation. The structure, however, varies a lot from language to language.

For instance, here’s how the expression add 2 and 4 is commonly written in the language called LISP:

(+ 2 4)

and in a language called Java it looks closer to the mathematical notation:

2 + 4

Language designers have specific reasons for why a language looks the way it does. This is also a matter of trade offs, since a language’s design impacts the speed of execution, the programmer’s productivity and the ease of maintaining the software.

The Ruby programming language

We will be using the Ruby programming language, developed by the japanese programmer Yukihiro Matsumoto in 1995. The language itself is a standard that describes whether a text file can be interpreted as valid Ruby code or not.

To gain familiarity with Ruby, we will be using the online interpreter available at tryruby.org. Afterwards, we will be doing the same thing on your computer.

Attention! In the following examples you do not need to enter the preceeding > sign. Here and in the other guides we will use it to show commands we enter in an interactive interpreter.

Let's start with the interpretation of a simple instruction. Type in 10 * 32. You should get the result:

> 10 * 32
=> 320

This means we introduced a valid instruction that was evaluated, and returned a result. But what if we tried to compute a factorial, let's say with 5!?

> 5!
=> The code you submitted was not valid Ruby.

Oops! It looks like the computation of factorial is not a default component of the Ruby syntax.

Variables

When writing programs there are several things that you can see in almost any program.

A variable is a label that can be put on different pieces of data. Let's type the following in the online interpreter:

> best_community = "Rails Girls Chisinau"

What we did was create a new variable, called best_community and assign to it the string "Rails Girls Chisinau". We can create as many variables as we want:

> favorite_number = 25
> best_color_name = "red"

We can now ask the interpreter for the current value of the variables:

> best_community
=> "Rails Girls Chisinau"

Since best_community is a variable, we can reassign it.

best_community = "Ruby Moldova"

Now, if we ask the interpreter what is the value of best_community, we will get

> best_community
=> "Ruby Moldova"

Functions

A function is a transformation applied to some data. In real life, if we want to multiply a number by two we apply a transformation to that number. In Ruby we can express that as follows:

def double(a)  
   return a * 2
end

We have just defined a function called double. This function can take an argument. For simplicity we called this number a. Inside the function, the function argument, a, will behave like a variable. Our function returns the value of the variable multiplied by 2.

What we have above, is a function definition, or the description of a series of instructions, performed only when this function is actually used or called.

To see the function in action, you will first type the definition above into the interpreter. The function is defined and now you are able to use it.

Type double 6 and you will get

> double(6)
=> 12

You can also type double(6), if you like it more that way. Either way, you will get the returned value 12 displayed on the console. See, Ruby works!

However, if you try to call a function you haven't yet defined, such as triple 6, you will get an ugly error message. In time, we will learn to read this message and understand that the function triple has not yet been defined.

Let's try another function:

def greet(name)  
  print "hello, " + name
end

What do you think it will do?

Hint: if you get in trouble, try using double quotes "like this" around a text, to actually show the interpreter you are using text instead of trying to use a variable or method defined above.

Conditionals

Sometimes you need to perform a computation based on a condition. For instance, you can ask the user for their age and if they are younger than 13, send them away:

def check_age(user_age)
  if user_age < 13  
    print "go away"
  else  
    print "welcome!"
  end
end

What we just did is create a function called check_age.

Inside the function, we use the variable user_age. If the value of the variable user_age is smaller than 13, then you print on the screen "go away". Otherwise, print "welcome!".

Let's see how it works:

> check_age(14)
=> "welcome!"

and

> check_age(12)
=> "go away"

So far, we have taken only a glimpse at the Ruby programming language. We will be using Ruby while working on the application, so, if you have the time, please make yourself at least superficially familiar with it.

What’s an operating system?

The operating system is the most important program that runs on your computer. It is a system program that manages all the other software and the hardware of the computer.

Usually, on your computer many programs run simultaneously, each needing to access the central processing unit, memory, and storage. The operating system coordinates these interactions, making sure that each program gets what it needs.

The operating system is the container where other programs are running. The programs themselves are written in a large variety of programming languages.

CLI - The Command Line Interface

The window, which is usually called the command line or command-line interface, is a text-based application for viewing, handling and manipulating files on your computer (just like Windows Explorer, but without the graphical components).

Other names for the command line are: cmd, CLI, prompt, console or terminal.

On Windows to start the command line interface go to Start menu → All Programs → Accessories → Command Prompt

On Linux (Ubuntu for example) command line interface is probably under Applications → Accessories → Terminal, but that may depend on your system.

You now should see a single-coloured window (white or black by default, purple on Ubuntu) that is waiting for your commands.

Windows command prompt

On Windows, you will see a > sign, like this:

>

On Linux this will be the $ sign:

$

In many online tutorials or books, the commands that should be entered in the terminal are prefaced by one of the signs above, but this does not mean you have to enter this character from your keyboard. For example, to run an example like $ cd, you will only have to type in cd.

Homework

Practise Ruby

We encourage you to take the basic course on Ruby you will find at tryruby.org or the free course on CodeSchool. You only need to take the course up to level 3.

Command line interface

Locate and open the default command line interface program on your computer's operating system.

results matching ""

    No results matching ""