Photo illustration by Joshua Fuller | Unsplash

Intro to Ruby: A Programming Language

Tomas Gonzalez
7 min readSep 15, 2020

--

Note: This write-up is for you if you are first learning about Ruby, or need an overview of Ruby basics. Please comment if you have any clarifications or constructive feedback!

If you have done your research, you probably already know that Ruby was publicly released in 1995 by its creator Yukihiro “Matz” Matsumoto. The next stable versions of Ruby were released in subsequent years.

But what exactly is Ruby?

Great question! Ruby is a “dynamic, open source programming language with a focus on simplicity and productivity.

Oh… sick! But what does THAT even mean?

Also a great question! First off, a programming language is a set of instructions that produce different types of output. Indeed, humans are responsible for programming, writing code, algorithms, and basically all of the instructions for computers to process and interpret when their methods or functions are called upon.

You and I make these calls everyday from our mobile devices, laptops, and personal computers! Whenever you visit a website, app, or game online, you are making a request to another computer, or server, to render the functions in a program, which, at some point, were written by a software engineer.

Ohhh… So that makes Ruby…

Right! Ruby is an “object-oriented programming” language that allows software engineers to write code, or instructions, to create web applications.

Object Oriented Programming: OOP

Now that we have a basic understanding of what programming and programming languages are, let’s take a dive into object-oriented programming.

In OOP, every physical thing can be represented as an object. In Ruby, classes are considered the blueprint from which individual objects are created.

If we consider a car as our class, we would define it in Ruby as such:

class Carend

A particular car might have attributes like make, model, and color. In Ruby, we would define those attributes in an initialize method. “attr_accessor” allows us to grab that information or access it(more on that later):

class Car  attr_accessor :make, :model, :color  def initialize(make, model, color)
@make = make
@model = model
@color = color
end
end

Let’s talk about some of the different variables you are seeing in this class.

Local Variables: These variables are only allowed to be called within a method.

Instance Variables: These variables can be accessed across methods in any instance or object. They’re preceded by the @ sign.

Awesome! You’ll notice in the method above we don’t actually have any real description of a new vehicle. However, what we do have are the ingredients to make a new car. Let’s make one:

class Carattr_accessor :make, :model, :colordef initialize(make, model, color)
@make = make
@model = model
@color = color
end
end#in our IRB we can create a new car using the ingredientscar1 = Car.new("Honda", "Civic", "blue")=> #<Car:0x00007fb3d79de080 @make="Honda", @model="Civic", @color="blue">car1.class
=> Car

Whoa! What just happened?!

I’ll tell ya! We created a new Object, which is an instance of our car class and gave it some actual attributes. We also temporarily saved the new instance to a variable car1. You can think of each attribute as an additional method of the car that we can now call upon to get information. (**This is where attr_accessor comes in — attribute accessors allow us to see read and write that info)

Let’s explore that below:

reading info of car1car1
=> #<Car:0x00007fb3d79de080 @make="Honda", @model="Civic", @color="blue">
car1.make
=> "Honda"
car1.model
=> "Civic"
car1.color
=> "blue"

Hmm… A Honda Civic though? Seems lame, let’s change this to something much cooler.

writing/altering info of car1car1
=> #<Car:0x00007fb3d79de080 @make="Honda", @model="Civic", @color="blue">
car1.make = "Ferrari"=> "Ferrari"car1.model = "812 Superfast"=> "812 Superfast"car1.model = "Hot-Rod Red"=> "Hot-Rod Red"car1=> #<Car:0x00007fb3d79de080 @make="Ferrari", @model="812 Superfast", @color="Hot-Rod Red">

Whoa whoa whoa. That’s cool and all, but before we start giving ourselves Ferrari’s, let’s learn how to walk(or drive)!

Variable Assignment in Ruby

Everything in Ruby is an object, and objects are an instance of the class. In the example above we created a custom class of Car. Then, we assigned car1 to a new instance of that class with its attributes.

Let’s step away from our car class and break this down into two topics: Variable Assignment and Objects.

First let’s discuss variable assignment. A simple way to show this is to do math! Follow along below:

IRBnumber = 10 
#number is being assigned to 10
=> 10
#10 gets returned
another_number = 20
#another_number is being assigned to 20
=> 20
#20 gets returned
number + another_number
#we add number + another_number, which are assigned to 10 and 20
=> 30
#this returns our expected value of 30
____________________________________________________________________
# THIS IS REALLY HELPFUL SO PAY ATTENTION
# -- you can save the last return value to a new variable using the following line of code
new_number = _
#assigns new_number to the previous return value
=> 30
#returns the value of new_number
new_number
#calls new_number
=> 30
#returns the value of new_number

Variables are important because they allow the programmer to assign labels to data. Variables can also be referenced if we’re looking for specific information. Also note: when we assign a variable with two or more words we use what is called snake_case.

You can imagine another programmer looking at my code and wondering, “Hmm, why did Tomas just label integer ‘number’?”

Right. It’s not very descriptive at all. But if I wanted to make this piece of data important, I could label an integer 28 as “age,” and that would be more useful and informative.

For helpful info on variable assignment, click here!

Objects in Ruby

Next, let’s talk about objects.

Again, everything in Ruby is an object, which, again, are instances of a class. We can see this when we look at the different data types in Ruby.

The data types in Ruby are: Strings, Integers, Booleans(represented as TrueClass and FalseClass), Arrays, Hashes, and Symbols.

IRB"Tomas".class=> String1.class=> Integertrue.class=> TrueClassfalse.class=> FalseClass[1, 2, 3, 4, 5,].class=> Array{my_name: "Tomas}=> Hash:hello.class=> Symbol

Since Ruby is an object-oriented programming language, all of its supported data types are implemented as classes.

It’s important to note that all classes have methods that can be called upon them.

In fact, when we called .class upon our examples above, we were calling the .class method on them.

Each object or instance of a class has a set of methods that can be called upon it.

For more info on data types in Ruby click here!

Let’s tie it all together — instance and class methods

When talking about methods, there are two different types. Instance and Class methods.

We’re going to do two things: First, I’m going to show you the difference between a class method and instance method using an integer example. Then, we’ll finish it out with our car example.

Let’s check out this info below:

IRBInteger.sqrt(9) # here you will notice we're calling the sqrt on the class of Integer and passing in an argument of 9=> 3Integer.sqrt(16)# here you will notice we're calling the sqrt on the class of Integer and passing in an argument of 16=> 4
____________________________________________________________________
____________________________________________________________________3.odd?=> true4.odd?=> false3.even?=> false4.even?=> trueInteger.methods.count
# the class of Integer has 111 different methods you can call on it
=>1113.methods.count
# the instance 3 of Integer has 143 different methods you can call on it
=> 143

In the example above, Ruby does not allow you to call the class sqrt() method on an instance of an integer. You would need to pass the instance of the number as an argument into the class method of sqrt(). Same, you can’t call the instance methods - odd? or even? - on Integer, because it’s not really a value yet — it’s only a class.

Cool, let’s look at our Car example now. If you remember we left off modifying our car into a Ferrari. But how did we manipulate that data?

class Carattr_accessor :make, :model, :colordef initialize(make, model, color)
@make = make
@model = model
@color = color
end
end#in our IRB we can create a new car using the ingredientscar1 = Car.new("Honda", "Civic", "blue")=> #<Car:0x00007fb3d79de080 @make="Honda", @model="Civic", @color="blue">car1.class
=> Car

You’ll notice above that we created a new class Car and gave it an initialize method to produce some instructions on how to make a new instance of it. Then we called the class method .new to create a new instance of the class!

We then saved that instance of a car into the variable car1. Because we gave the initialize method the attributes we want of make, model, and color(along with an attr_accessor), this allows us to use those as instance methods to change the attributes for car1.

We used those methods to change our blue Honda Civic into a Hot-Rod Red Ferrari 812 Superfast. See below:

car1
=> #<Car:0x00007fb3d79de080 @make="Honda", @model="Civic", @color="blue">
car1.make = "Ferrari"=> "Ferrari"car1.model = "812 Superfast"=> "812 Superfast"car1.model = "Hot-Rod Red"=> "Hot-Rod Red"car1=> #<Car:0x00007fb3d79de080 @make="Ferrari", @model="812 Superfast", @color="Hot-Rod Red">

Conclusion

That’s it! In this article we learn a little about what programming and programming languages are. We also learned that Ruby is an object-oriented program with classes and instances of those classes. Ruby is an extremely intuitive, clean, and fun way to springboard into learning how to program!

--

--

Tomas Gonzalez
Tomas Gonzalez

Written by Tomas Gonzalez

Experienced Ruby on Rails and JavaScript based programmer with a background in journalism, creative content, and multimedia-marketing. I love coffee and food!

No responses yet