Basics

Before Rubymotion, we need to learn Ruby. Ruby is an awesome and flexible language that helps us make apps in less time and with less code.

We won't cover all features of this programming language here, so I would suggest reading some other books to get familiar with it. If you already have some programming experience, then Ruby will be extremely easy to learn.

It would be a good idea to read a Wikipedia article about Ruby.

Before we get started, let's talk about some basic things.

Each app is a set of text files, that compiles into a working program. It is important to make sure that text does not have any logical or grammatical mistakes. If it does, compiler (or interpreter) will throw an error, and an app will crash, or even won't launch.

However, some parts of the code can be skipped by a compiler. To ask the compiler to skip some lines inside of a file, we make a "comment". Comment in the source code is just a random text that will not be interpreted as a part of the source code. Developers use it to leave some notes to their coworkers, to describe how some part of the code works, or just to temporary disable some functions.

In Ruby commented lines start with a # symbol. In the next example, the first line will be executed, and the second will not:

        10 + 1
        # 10 + 2
      

In this book, we will use comments to add some notes to the source code, so it will be easy to understand what does the code do. For example:

        x = 10
        y = 20

        # calculate average:
        avg = (x + y) / 2
      

Methods

We will talk about methods later, and for now, you can think of methods as some actions that objects can do. For example method push will push a new value to a given array:

        array = [ 1, 2, 3, 4 ]
        array.push(5)
        # => [ 1, 2, 3, 4, 5]
      

if/else statements

There are lots of cases where programs need to do different things: if we received correct data from the server - show it, otherwise show an error. If user's username exists in the database and password is correct - open the app, otherwise show an error, etc. In Ruby, this can be achieved with if/else statement:

        if authorised
          # show app
        else
          # show error
        end
      

There are 3 logical operators: && (means 'and'), || (means 'or'), ! (means 'not'). Next example can be red as "can show app if username is "example@gmail.com" and password is "123456".

        if username == "example@gmail.com" && password == "123456"
          # open the app
        end
      

Let's make a more user friendly example, that will show why user can't login:

        username_correct = username == "example@gmail.com"
        pass_correct = password == "123456"

        if username_correct && pass_correct
          # open the app
        elsif username_correct && !pass_correct
          # ask user if he forgot the password
        else
          # seems like username is also incorrect, show error
        end
      

Syntax

Ruby has a simple and flexible syntax. It is not important whether you use spaces between values:

        x = [ 1, 2, 3 ]
      
or not:
        x=[1,2,3]
      
You can even omit brackets:
        "string".is_a? String
        "string".is_a?(String)
      

We became familiar with Ruby syntax, and we're going to learn what is a variable and how can we use it in the next chapter.

Book Index | Next