What the H*ck is... Ruby?
Ruby is a programming language. Developed in the 1990s by a guy named Matz. It was designed with Object Oriented Programming (or OOP) in mind, a particular programming pattern. Ruby is built for human readibility. It was one of the first languages I learned as a programmer, partially because it reads like English.
Examples
Where to start? First, let’s go easy. Math is relatively easy.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
6 + 2
# This is addition, so it returns 8
6 - 2
# This is subtraction, so it returns 4
6 * 2
# This is multiplication, it returns 12
6 / 2
# This is division, it returns 3
# There's a lot more math that Ruby can do,
# like decimal points (called floats in Computer Science),
# exponents, paranthetical operations and more,
# but you can look those up yourself.
Simple right? The real great thing though is how it reads when you actually build something. So here’s what we call a method in Ruby. It’s just a bit of code that can be called again and again as needed.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# First, we define it
def add_three(number)
# See the word "number"? That's called an argument.
# Arguments get passed and then things can get done to it.
# Like adding 3
return number + 3
# Finally, we make sure that the computer knows this is the end.
end
# Then we call it!
add_three(5)
# It will return 8!
# Call it again! But different!
add_three(3)
# It returns 6!
Reads well. It really does. Ruby allows for the programmer (which could be you), to write descriptively. Which is a nice way of saying that you and I can name the variables and methods in a way that makes sense to a human rather than what only makes sense to the computer. This is where Ruby shines (pun intended). It’s designed for the human to read and understand first, rather than making the human read the way a computer would.
We can’t write about Ruby without touching OOP though. Ruby uses something called classes and the classes hold variables and methods. When you make a class, you also have to make an instance of that class, which is a way of holding particular bits of data. Below, I’m going to write a class called Human
and have it do things.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class Human
# This will get used later,
# All you need to know right now is that this is how we store data
def initialize(person_name, saying)
# These variables below are called 'class variables'.
# They are a way of storing data so that other methods can see them.
# As long as you start the variables with 'self' or '@',
# and hand them something to hold, other methods can get them.
@person_name = person_name
@saying = saying
end
def say_name
# puts is a way of showing things in the terminal.
# This case, we are making the Human say hello.
puts "Hello, my name is #{@person_name}."
# Notice the quotes? That means that the words are a "string",
# a way of storing words.
# The '#' and brackets are a way of putting variables in a string.
# Without those quotes, the computer will think
# there's a bunch of variables called "Hello" and so on.
# There isn't so it will break.
end
def say_saying
# Now we just puts the saying variable. No interpolation here.
puts saying
end
end
# There's our human! We could make him walk as well as talk,
# but there's no need.
# How do we make a human then? We made a generic human,
# but how do we name a particular human?
# We need to make an instance of the class, like so.
marsha = Human.new("Marsha", "Hey there!")
# We made a variable so we can call it at any point.
# Then we gave her a name and a saying.
# If we switched those two strings, it would make her name
# "Hey there!" Which is silly. Order matters!
# Now let's make her talk!
marsha.say_name
# returns "Hello, my name is Marsha."
marsha.say_saying
# returns "Hey there!"
Does that make sense? I hope so. If not I’m sure you’re close. There’s lots of places to start, to be perfectly honest. This is just a starting point. I’ll leave some links for you, and maybe Ruby will be your jam, as it is mine!
Further Links
- Book/Link: Learn Ruby the Hard Way (Don’t be intimidated, it’s actually quite fun.)
- Book: Learn to Program by Chris Pine (Not the actor, though that would be cool.)
- Link: Learn Ruby in Y Minutes (Great resource if you already know how to program.)
- Documentation: All of it (Jump into the deep end!)