Eat Your Variables - Learn Ruby The Beginner Guide An Introduction to Ruby Programming (2015)

Learn Ruby The Beginner Guide An Introduction to Ruby Programming (2015)

Eat Your Variables

Close your eyes. Now imagine you're able to read this with your eyes closed. I would tell you to pretend you're looking down at a bowl with one spaghetti noodle. The noodle is our string. The bowl is our variable.

Variables represent or symbolize other objects. If I wanted the spaghetti (or whatever else is in the bowl) I would look in the bowl for it.

Let's say we wanted to create a shortcut for writing the phrase, "How deep does the rabbit hole go?" First, we would create a variable name. We will call it the_question. Try to come up with names that give a clue as to what the variable is and what it's for. When creating a variable name you need to make sure to separate words with underscores. Spaces will not work. After we create our variable, we will set our string equal to it.

the_question = "How deep does the rabbit hole go?"

We've now assigned our string to the variable the_question. We can call it like this:

puts the_question

How deep does the rabbit hole go?

I have an itch to simply print out the w in the word How. I must scratch it.

print the_question[2]

w

With brackets we can target specific characters in our string. The [2] at the end will print the third character in the string. Like many other programming languages, Ruby starts counting at zero from left to right.

print the_question[0]

H

We now want to print the g in the word go. It seems arduous to count all the way through our string to figure out its position. Luckily we can pull this little trick:

print the_question[-3]

g

By using the minus sign we're telling our pal Ruby to enter through the back door. The last character in a string holds the position -1. It follows that the character in front of it is -2 and so on.

We're going to change our food order now. We want to order a whole rabbit. We won't worry about using the back entrance in this one. (Unless you hear the call of adventure!)

String for reference: "How deep does the rabbit hole go?"

print the_question[18..23]

rabbit

We can select the scope of what we want printed by using two dots between the first and last target. We could even exclude the last item by using three dots.

print the_question[18...23]

rabbi

This rabbit seems a little small. Take off your crocs and let's dip our toes into a new method.

print the_question[18..23].upcase

RABBIT

The method .upcase will capitalize that particular instance. Our bunny is too large now. We can fix that.

print the_question[18..23].capitalize

Rabbit

From now on we want the_question to print every character capitalized without having to type .upcase at the end every time. We can do this by changing the string in place.

the_question.upcase!

We simply type the variable name and then give it a tail of .upcase!. Make sure to include the exclamation point. That's the part that changes it in place.

puts the_question

puts the_question[0..7]

puts the_question[-12..-3]

HOW DEEP DOES THE RABBIT HOLE GO?

HOW DEEP

BIT HOLE G

Each particular instance can still take back control.

puts the_question.downcase

how deep does the rabbit hole go?

Glorious. If you're a vegetarian feel free to release your rabbit now.

Math

Here I will introduce you to your next data type. Integers are whole numbers. Your basic 4, 56, 91, etc. We're going to use variables and integers together to do some math.

In the editor, create values for variables x and y:

x = 21

y = 50

This next part is simple:

puts x + y

71

Can you imagine scenarios where we would want the sum of both x and y to be stored as its own variable? That's what we'll do here:

z = x + y

puts z

71

Adding things is fun. It's pretty basic though. Let's take a breather and discuss the rest of what's called arithmetic operators. Take a look at these:

+ Add

- Subtract

* Multiply

/ Divide

** Exponential

% Modulo

We'll discuss exponential and modulo quickly. An exponential is simply a number multiplied by itself a certain amount of times.

4 ** 3 is the same as 4 x 4 x 4

When you use modulo, Ruby will return the number that is the remainder of a number divided by another number.

10 % 94

4

Ten goes into ninety a total of nine times with four left over. Four is what modulo will return.