Starting your first role as a software engineer and having no knowledge of the programming language used in your organisation sounds like a nightmare at first, as thoughts of the imposter syndrome that many of us seem to suffer from these days start to take over. But worry not. At FreeAgent the environment is very friendly and relaxed, allowing you as a new joiner to focus on learning and integrating with the rest of the team while everyone is also available to guide you through and answer any questions.
Many of us at the start of our career are either coming from an academic background, a bootcamp or self-learning and will probably have some experience with Python. It’s widely known as a beginner-friendly language because it’s concise and easy to read and understand.
In the rest of this article we will explore some of the main similarities and differences between Ruby and Python which should hopefully provide you with an initial idea of what Ruby is and make you feel a bit more familiar with it later on as you progress with your learning.
Both are high-level languages
Both Python and Ruby provide a high level of abstraction hiding away low-level hardware details such as memory management. Both also emphasise human-friendly syntax that’s highly readable and offer built-in functionality that should cover any task. This allows us to be more efficient while coding and thus more productive because we don’t need to worry about low-level details whilst being able to reuse built-in functionality.
Both Python and Ruby are dynamically typed languages. What this means is that variable and function types are determined at runtime. As a consequence, we don’t need to worry about explicitly declaring variable types while writing code. This allows for more flexibility. For example, the same variable can be assigned to different values of varying types. It also enhances readability and keeps the syntax at a minimum.
Both Python and Ruby are designed around object-oriented programming principles and hence everything is treated as an object. Both Ruby and Python allow us to define our own classes with attributes and methods. As a result, many of the advantages of object-oriented programming become apparent in both cases. These include abstraction, encapsulation, inheritance and polymorphism.
Both are used in web development
Both Python and Ruby have popular web frameworks. Python has Django which is a Model-View-Template framework and is used to support applications such as Spotify, Dropbox and Instagram. Ruby has the Ruby on Rails framework which uses the Model-View-Controller architecture and is used to support applications such as Airbnb, GitHub and of course FreeAgent. Both have a similar file structure and transitioning from one to the other is not a big struggle.
Readability and white space
As already mentioned, both programming languages strive to be as readable as possible. Both make use of white space and indentation to define code blocks and give structure to the code. This allows for clean-looking code that is not just readable but is also consistent.
As with any new programming language, learning the syntax can be quite daunting at first. While the two languages are quite similar when viewed from a higher level, there are some differences when it comes to the syntax.
In Python indentation is enforced and is used consistently to indicate the beginning and end of a block of code. The convention is to use 4 spaces. Here is an example of a conditional statement. Statements end with a colon and the block of code within them must be indented.
The same example in Ruby requires an additional line for the end keyword, which is needed to end a block of code. While indentation is also used, it is not mandatory and is usually done with two spaces instead of four. Also notice the difference between the Python print function and the Ruby puts function.
In Python the use of parentheses is required when calling methods, even if no arguments are passed.
As seen in the example below the .lower() method is called without any arguments but the parentheses are still required.
The same example in Ruby would look like this:
As you can see in the example, calling the .downcase method without arguments does not require the use of parentheses. Even when arguments are needed – such as when puts is called – there is no need to add parentheses.
Both Python and Ruby have native boolean values. In Python they are written True and False, whereas in Ruby they are written as true and false. Other than the explicit boolean values, both languages have a variety of ‘truthy’ and ‘falsey’ values. In Ruby the only ‘falsey’ values are false and nil and even 0 evaluates to true which would not be the case in Python.
Iterating over a collection of items is a core programming activity and both Python and Ruby offer various ways to perform iteration.
The most common pattern for iteration in Python uses the for loop to iterate through a collection of items.
Another common iterator pattern in Python uses the built-in range() function that generates a sequence of numbers.
In Ruby, the most common way to achieve iteration is by calling the each() method.
To iterate a specific number of times, similar to Python’s range function, we can generate a range using the (a..b) literal. Note that the upper-bound is included here as opposed to Python’s range() function.
Alternatively, you could make use of the times() method as shown below.
Many people believe Python is the most beginner-friendly language and the easiest to learn when first introduced to programming. It has a very simple syntax that is easy to read and understand. Ruby on the other hand, while also being highly readable, is not as easy to learn and requires some getting used to.
Python is generally more popular than Ruby as it is more widely used, has a larger community and is used more often in the industry. There are a few reasons for this, the main one being that Python is very versatile and is used in various domains such as web development, automation, and most importantly, data science. Data science has gained significant traction in the last few years and Python offers an array of libraries that can aid in the process such as NumPy, pandas and TensorFlow. This has made Python one of the most in-demand programming languages in the industry.
Having a solid background in Python or any other high-level programming language should make transitioning to Ruby a relatively simple process as the language is meant to be developer-friendly and easy to read and understand. Once you become a little bit familiar with the syntax, the rest should be a piece of cake.