Day 2: Data Types and Variables

How programming works

First, let’s figure out How Programming Works

Don’t be scared of coding. It’s actually pretty simple. Any program is just code running by an interpreter inside an operating system. Let me explain:

  1. Code is text written according to a certain (Ruby) syntax which is stored into a regular file.
  2. Interpreter (Ruby binary) is the program that reads code and runs it. It takes some actions according to instructions written in code.
  3. Operating System (Mac OS X) is the software installed on our computer that runs the interpreter.

That’s it! So all we need to do is to write some ruby code and run it. Let’s figure out how to write code.


Variables are like labels. Let’s say you put all your old stuff in a box. You don’t want to forget what’s in it. You put a label on the box with the name old_stuff. Next time you want to look at your old stuff, you can refer to the box named old_stuff. Same analogy works for programming. You create a variable named old_stuff. You assign it a value of “action figures of Ninja Turtles” which is a string of text. When you need to look at old_stuff in your program, you can reference it by the variable named old_stuff.

old\_stuff = "action figures of Ninja Turtles"
puts "Check this out: " + old_stuff

Note that every programming language has it’s own convention. It’s a set of naming rules that programmers agree to use for the sake of consistency. Here is our first Ruby convention. A variable name in Ruby is always lowercase separated with the underscore symbol. For instance: variable or variable_with_many_words.

Try it out!

Seriously, try it, right now. Go to your terminal and run irb (Interactive Ruby). This tool will run the code you type in the real time. It’s amazing!

$ irb

Now just type in the ruby code from above line by line. The same way as you would type regular text. As soon as you hit Enter, your code gets executed and you will see the result. If you typed it correctly, you should see this:

Ruby variables in IRB

Here it is. We wrote our first lines of code. Congrats!

Data Types

Meet Data Type Cat.

Data Types Photo by mr_t_in_dc

Variable has a value. The type of this value is called a Data Type. In other words, if a variable is a label on a box, a data type is the way to describe what’s inside the box.

You might notice that the condition might be evaluated to either true or false. Here is our first data type called Boolean.

Ruby is a very simple language. There are just 3 basic data types:

  1. Boolean. A way to say yes or no. There are only two boolean values: true or false.
  2. Number. A fixed or floating point number. Examples are 5 or 5.25.
  3. String. An arbitrary string of text. Example: “Text inside double quotes” or ‘Text inside single quotes’

Go ahead and try it in your irb

variable = true
variable = 5
variable = 5.25
variable = "String of text"

From now on, I highly encourage you to type code in. You’ll learn way better by typing code yourself instead of just copying and pasting.