For about six years, I worked primarily on the Ruby on Rails stack. For me, Ruby was (and still is) a great language for learning programming concepts like test-driven development and object-oriented design patterns. Its syntax is “human-friendly” and great for beginners to read and understand.
Recently, I moved back over to Python and Django. Python is a much more direct programming language than Ruby. It emphasizes simplicity, making everything as obvious and explicit to the programmer, even if it’s verbose.
Both Ruby and Python, though different in their philosophies, are a joy for programming! They’re easy to learn and enable software developers to be productive very quickly. However, there are some features of Ruby I miss while working in Python. These are just a few I thought I’d share.
Functional approach to working with lists/arrays
In Python, iterating through lists often involves a standard
for-loop. Python also has a nice feature for building lists called list comprehensions.
Ruby’s way of operating on arrays feels a bit more intuitive. Its functional-first approach is very straightforward to read and write, especially when chaining multiple operations together.
Ruby also has many built-in methods for working with native enumerable data types. This makes working with arrays really easy from the start!
Explicit private methods
Encapsulation is an important concept in object-oriented programming. The ability to define and enforce private methods lets programmers declare the intended interface for any particular class.
There are philosophical reasons for Python’s lack of private methods. However, a similar effect can be achieved using a Python concept called name mangling. Just prefix “private” methods with double-underscores.
In Ruby, private methods are declared with the
Although the functionality is somewhat the same, I prefer Ruby’s explicit
private keyword over Python’s naming convention.
Modules and mix-ins for abstracting functionality
Building abstractions and sharing them across classes is a common way to keep code DRY (don’t repeat yourself).
In Python, integrating cohesive logic from more than one abstraction is commonly achieved with multiple inheritance.
Ruby doesn’t support multiple inheritence. However, it accomplishes this behavior by implementing a mechanism called mix-ins.
In Ruby, modules are “mixed in” with classes, making objects “act like” the desired abstractions. In Python, classes are “inherited”, making objects “become” the desired abstractions. Creating and sharing modules for the purpose of encapsulating behavior feels cleaner and more semantic than creating classes.
In-line variable string interpolation
Interpolating variables in Python can be achieved with
String interpolation in Ruby is similar, but Ruby has the advantage of interpolating variables in-place.
It’s a subtle difference, but it makes for a better reading experience.
Note: Those who are fortunate enough to be using Python 3.6+ can get a similar experience with
There is no shortage of articles on the web comparing and contrasting Ruby and Python. As with anything in programming, as long as things are done idiomatically, both are great for developing software.
Do you also have experience with Ruby and Python? Share your thoughts and comment below!