Sitemap

Learning Ruby

4 min readFeb 15, 2022
Press enter or click to view image in full size

Ruby having very similar appearance in functions to other programming languages, it isn’t uncommon for fellow programmers (like yourself) to take a liking of Ruby because it is made to “make programmers happy” -Yukihiro “Matz” Matsumoto (designer of Ruby). It was developed that would be simple to use, elegant to write, but capable of building endless complex things.

If you were thinking hard on whether to learn Ruby as a side gig, or have had a genuine thought on learning the framework, consider these examples below to see if it still piques your interest.

Methods

One of the first things you likely have learned was how to write functions. Here are some differences on functions from Ruby, compared to JavaScript:

A typical function of JavaScript

Compared to Ruby, which can look very familiar, yet simple to look at:

“puts” is like console.log

Notice the curly braces are not present, and the function is enclosed in a “code block” of the words of “def — end.” “Def” keyword is used to identify this code as a method. In JavaScript, the function name is conventionally in camel case (i.e. myFunction). However, in Ruby, it is in “snake case” (my_method).

In Ruby, whatever the last line of a method is will be the return value. You can use the word “return”, but it is practiced by doing an implicit return instead.

Enumerables: Method Syntax

Just like in JavaScript, in order to iterate over all elements in an array, we need to access every piece of data. As such, there are several built-in methods for arrays that help with iteration, each of which comes with its own use cases:

  • forEach: access each element from an array. Returns the original array.
  • map: access each element from an array, and return a new array, usually by transforming (mapping) the values to some new value.
  • find: access each element from an array, and return the first value that matches some criteria.
  • filter: access each element from an array, and return a new array of all values that matches some criteria.

Ruby also has methods that can be used on arrays (and hashes) and give similar functionality- just different syntax.

Get Dan Cheong’s stories in your inbox

Join Medium for free to get updates from this writer.

In JavaScript, you may see something like this:

As with Ruby:

Notice we don’t have a “def” at the beginning. That’s because we have a “do” before the |str|.

The “| |” are called “pipes.” When invoking an enumerable method like #map, the variable name inside the pipes acts as an argument that is being passed into the block.

Self

Much like the keyword in JavaScript “this”, in Ruby, Self is just that- a special variable that points to the object that “owns” the currently executing code. It is as if Ruby program asking, “who am I right now?”

Object Orientation in Ruby

Object-oriented programming (OOP) is a type of programming based on the concept of “objects”, which can contain data, in the form of fields, and code, in the form of procedures (often knows as methods). As for Ruby, it is a powerful object-oriented language used by popular applications.

Ruby comes with a few types of Objects to get us started, things like Integer, String, Array, etc. We call these base types of Objects “primitives.” But what if we wanted to create a new type in our programming universe, or a new kind of object for code? That’s what the class keyword and object orientation allows us to do.

.new method “instantiate” or create a new instance of a new Dog.

Classes are the blueprints that define the behavior and information our objects will contain. They let us manufacture and instantiate new instances.

As you can see, there are some aspects in Ruby that may seem very familiar to you. Many more were not covered here, but if it interests you, Ruby and Ruby on Rails offers great resources on their website.

For further information, please visit rubyguides.com for extensive guides on various topics regarding Ruby. Thanks for reading!

--

--

Dan Cheong
Dan Cheong

Written by Dan Cheong

A novice Software engineer-to be, US Army combat vet, prior police officer, full time leisure guitar player/singer and a wanna-be professional snowboarder