Ruby is a friendly and widely used programming language. It started with a programmer named Yukihiro Matsumoto, based in another program called Pearl. According to the book The Wide World of Coding, the origin of the language is this:

“As a high school student in Japan, Yukihiro (Matz) Matsumoto taught himself to code. In college, he worked in a programming language lab while majoring in information science.

“Despite his years of experience, Matz felt frustrated by programming. He wanted to concentrate on his goals for the program, ‘not the magical rules of the language,’ he explained, ‘I just want to say, print this!’.

“When Matz realized the powerful, user-friendly language he wanted didn’t exist, he decided to create it himself. He started developing his own language, called Ruby, in 1993 with the goal of making programming feel fun and natural. Matz released Ruby in 1995, at the age of 28.” (The Wide World of coding. Chapter 3)

This is an inspirational story, and it doesn’t end there, he also made Ruby, an open-source program, and in his own words, “the goal of Ruby is to make programmers happy.”

Not everybody can write a new language, but we can MINASWAN (“Matz is nice and so we are nice”) like this acronym inspired by his kindness and generosity. This means Ruby programmers cooperate and work together.

The idea of this blog is to give a hint of how to approach Ruby. I will share what was the most difficult subjects to understand for me, and to help you get the mindset to understand and dive into the OOP (Object Oriented Programming). I will begin by explaining what is OOP and several other concepts.

I will give two examples in which I spent a lot of time trying to understand. Without any background in programming, it was difficult to click, but after spending quite some time coding with a lot of different examples, I finally got it.

Object-oriented programming (OOP) is a type of programming using the concept of ‘objects’. Objects are an abstraction we use to communicate, that contain data and code. The data are attributes or properties in the form of fields. The code it contains is methods, or procedures. Wikipedia.

Some important concepts in OOP are the four principles: encapsulation, abstraction, inheritance, and polymorphism.

What is encapsulation? Encapsulation is what allows variables of an object to be private. This means that to interact with an object, you can only use methods provided but not directly affect the data. This means your data can be read but not overwritten.

What is abstraction? Abstraction means you only show what is necessary and useful to show or interact with, and the rest stays out of sight. Abstraction allows for interaction between complex objects, because only the highest level of the mechanism is shown and the rest is hidden, and only the parts that involve interaction between objects, not internal functioning.

What is inheritance? Inheritance means forming a hierarchy of code where logic, methods, and fields can be reused easily. For example, you can derive a child class from a parent class that reuses the same fields and methods but then can also have its own unique properties. This ties into a helpful acronym: DRY (don’t repeat yourself).

What is polymorphism? Polymorphism allows for a parent interface to impose common methods on distinct child classes, but the child classes can keep their own methods so that the common method is implemented in a way specific to the child classes’ distinct methods. This way, classes which differ from each other can be worked with through the same parent methods. Like, if you had three different pets who eat three different types of treats, but you had the method of providing treats, then automatically the correct treat would be called for per animal. On the ‘give treats’ method, the cat would receive tuna fish whiskers, the dog would receive its Milkbone, and the fish would get fish flakes. The parent class is made adequate to the child class.

Number 1. Class Methods vs Instance Methods

A method gives an object functionality. A class method provides functionality to a class itself, while an instance method provides functionality to one instance of that class. A class can have many instance methods, and you can reuse methods within a class.

Consider the following Ruby class:

This would give in the following:

As developer Adam Lombard explains on his blog, “We cannot call an instance method on the class itself, and we cannot directly call a class method on an instance.” In other words, whenever in a method we use “self” (i.e. def self.name_of_method) means that this method is for the class itself, a class always starts with uppercase, class HelloWord.

Number 2 Between the Pipes

Whenever you want to write an instance method in which you need to iterate in an object or an array you will encounter the pipes | |.

Curly braces define an anonymous function. That function is called a block.

What’s found in between the pipes are the elements of that block. There may be several elements. Each time a block is evaluated, it will look for whatever matches the elements called. Only what matches is shown, and data that was evaluated but didn’t match is not stored. An argument is whatever you want to pass and evaluate. Arguments appear in method calls.

Here’s an example:

25.times {|x| print x} #=> 0 1 2 3 4 5…24 =>25

The method here is called ‘times.’ It is a built in method. In this case, the 25 is receiving the times method and it is passing up to 25 times. You are calling for X and everything that matches X is retrieved. In this case, it is all integers less than twenty five.

Here is a method that accepts a block (function) with two arguments.

In this example, the .each method calls the block every time for each key/value pair. The key is the first argument and the value is the second argument.

Print (puts) two times because we have two pairs of objects in the hash.

In more complex methods, be sure, when iterating, to make the name between the pipes something similar to what you are looking for.

In this case Article.all should have an array of articles, so between the pipes, let’s call it articles. This will help you to remember where you were if you came later or give a hint to another coder who is working on the same project.

There are many more interesting topics in Ruby, but the most important thing is to learn how to basic methods allow objects to interact with each other, and how classes can interact with each other. This allows us to build more complex and interesting projects. The next goal will be working with databases and learning how to store all this information. I’m also excited to learn how ActiveRecord will help to manage those connections, such as relationships and inheritance.

Thanks for reading my blog!



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store