Conceived in 1993 by Yukihiro Matsumoto, Ruby is a programming language that has been used for years in Japan before its popularity exploded worldwide in 2005. That happened especially with the invention of Rails, a web-app framework written entirely in Ruby. It was a real game changer for startups as it greatly reduced the complexity required to build a full-stack web application.
Since then, the language has been used to build more than 3.8 million websites , including behemoths such as Twitter, Shopify, Twitch, and Airbnb. So, if you’d like to pick up Ruby and join the millions of engineers that use the language across the globe, there are a few key concepts you should understand first.
A Brief Overview of the Ruby Programming Language
Ruby became one of the most popular programming languages partly because of its ease of use. The language is known as a developer’s best friend due to its minimal syntax that relies on plain English words and the use of blocks to write short and practical code.
Ruby is also an object-oriented language such as Python, JavaScript, and Perl to name a few. In such languages, every element is considered an object that you can modify. This syntax gives developers the possibility to customize anything. For instance, they can easily extend or modify the behavior of the language through the use of methods, classes, and subclasses.
However, as opposed to some other programming languages, Ruby was built with a user-centric approach. By design, it was meant to aid the software engineers first, not the machine. In this instance, it was made into a dynamically typed language.
Most Ruby code is run through the use of an interpreter which infers the data type of variables and object properties while it’s running. The most popular one, MRI — short for Matz’s Ruby Interpreter — has been the standard Ruby language reference for over a decade before YARV (Yet Another Ruby VM) became the official interpreter as of version 1.9. Thus, the latter will be used as a reference when talking about Ruby programming going forward.
The Basics of Ruby Syntax
In programming, syntax refers to the set of rules that make up a language such as words, symbols, and punctuation. Luckily for us, Ruby’s syntax is quite accessible. Here are the few core syntax concepts you need to master in order to start with the language:
Whitespace
Characters such as spaces and blank lines are ignored by the Ruby interpreter. They’re typically used by engineers to format programs and make them easier to read. Including them is then considered a good habit to take early on. However, in a few instances, some remain necessary. Adding or removing one might change the actions of a program.
Line endings
Semicolons and newline characters (\n) are generally used to terminate statements in Ruby. In the absence of any of those, the Ruby interpreter is left to figure out where the statement ends. This isn’t a problem per se when the statement fits on a single line, but when the statement is incomplete, the interpreter assumes that the statement continues onto the next line.
Identifiers
Ruby uses identifiers to name elements such as variables, constants, methods, etc. Identifiers can be formed of letters, numbers, and underscore characters. It’s important to note that Ruby identifiers are also case-sensitive. The same word written in all caps or lower cases will result in two different identifiers.
Reserved words
In Ruby, some words are keywords with a predefined meaning created to execute specific tasks. Although they can’t be used as constant or variable names, they can be used as method names. We won’t go over the 41 reserved words today but you’ll find below some of the most commonly used ones.
BEGIN | END | alias | and | break | case | class | def |
begin | end | module | next | nil | not | or | redo |
retry | return | false | for | if | while | defined? | then |
undef | true | unless | when | self | else | super | ensure |
NB: In Ruby, code must always start with the statement BEGIN and finish with the statement END.
Comments
As their name indicates, comments are annotations within Ruby code ignored at runtime. You can find two types of comments in Ruby code: inline and block comments. An inline comment starts with “#” and keeps going until the end of the line. A block comment starts with the keyword =begin and finishes with =end.
Ruby Modules Explained
A Ruby module is a grouping of constants, methods, and class variables. It’s generally used in one of two ways: as a namespace to avoid name clashes, or as a tool to implement a mixin facility. Used as mixins, modules allow code sharing across the application.
In a similar fashion to classes, modules serve to group methods and constants together without risking them being overrun by other methods and constants.
When incorporated into classes, modules serve as a way to make methods available to the class. This can come in handy if you have methods you want to reuse in certain classes while keeping them in a central spot.
As for labeling the modules, Ruby modules are always named the same way: they start with a capital letter followed by lower cases (ex: Module).
Understanding Ruby Classes and Objects
As with all object-oriented languages, Ruby involves two core concepts: classes and objects. To put it simply, a class is the model from which objects are formed. As we’ve seen previously, everything in Ruby is an object with a specific identity and behavior. These objects can be strings and numbers, as well as classes and modules.
To better understand both concepts, let’s look at a practical example.
Let’s say that the Computer class is the blueprint for creating computers. This class includes the entirety of attributes all computers have in common such as a monitor, a keyboard, a mouse, a CPU, a hard drive, a motherboard, and so on.
In this scenario, every individual computer is an object and, thanks to the Computer class, you can make as many objects as you like.
Classes in Ruby are always formed the same way:
- Their name starts with an uppercase letter.
- They’re introduced with the class keyword, and they finish with the end keyword.
Classes are somewhat similar to modules. They do hold methods in a similar manner that modules do. Their main difference lies in the fact that they can generate instances (objects) and have per-instance states (variables).
Of course, this piece was just a simple recap of the key notions to understand before jumping into Ruby programming. If you want to dig deeper, many free resources are available online. And if you need help with a Ruby project, you can always turn to our team of experts to support you every step of the way.