Python and Ruby are two of the best examples of the new generation of high-level languages which focus on simplicity and giving the programmer the ability to get things done fast, rather than syntax correctness and strict hierarchy (insert cough that sounds like “Java!” here).
First the similarities. Both are high-level, object-oriented languages. Both provide an interactive shell, standard libraries, multiplatform support across Linux, Unix, Windows and other operating systems, as well as persistence support. They are also both excellent for web development, more so when you take advantage of each language’s purpose-built web frameworks – Django for Python and Rails for Ruby. That said, Ruby on Rails is somewhat more popular as a web development tool than Django-Python. Python is favored more in the academic and scientific arenas.
But beyond that they also have some major differences. They also, surprisingly, have their passionate adherents and equally passionate detractors, which has resulted in more than one flame war that’s degenerated into insults and ad hominem attacks, and even into Godwin’s law. None of that here; we’ll delve into objective assessments and stick to what each language can or cannot do.
Ruby
Ruby was created in 1995 by the renowned Yukihiro “Matz” Matsumoto and was influenced by C, Perl, Java and C++. It is unabashedly object-oriented; in Ruby everything is an object. Some famous sites built using Ruby (on Rails) are Twitter, Hulu, Shopify, and Groupon.
From the very beginning, Ruby’s ethos has always been expressiveness, power and elegance. Its dedicated followers love it for its ‘principle of least astonishment’ - the belief that a language’s code should always cause as little confusion as possible for the developer. See this article for some excellent examples of how Ruby’s elegance and expressiveness stacks up against Java’s old-school way of doing things.
This guiding principle naturally leads to Ruby’s inheritance of the Perl philosophy of having more than one way to do the same thing. This is the main difference between it and Python, as we’ll see later. This in turn means that a method can have multiple names, and this could lead to confusion for newbies.
An often-touted superior feature of Ruby’s is its use of blocks. A block is a first-class object treated as a unit within the program. This ability makes the language very powerful, especially when combined with its other built-in forte – functional programming and use of lambdas. Also, Ruby takes the OOP concept to its limit. In Ruby absolutely everything is an object; even global variables are actually represented within the ObjectSpace object. Classes and modules are themselves objects, and functions and operators are methods of objects.
A quick list of features that work well in Ruby but not Python is listed below, and described in more detail here.
- Use of blocks
- Functional programming
- Fragmentation
- Hashable/ unhashable types
- Mutable strings
Python
Python was created in 1991 by Guido van Rossum, inspired by a multitude of languages – C/C++, java, Lisp, Perl and ICON. Some famous Python-built websites are Google, Dropbox, Reddit, and Youtube.
Unlike Ruby with its daring, somewhat adventurous nature, Python is somewhat conservative. Python abhors Ruby’s ‘many ways of doing something’ school of thought, which Ruby has in common with languages like Perl and PHP; instead Python has always stressed that there is only one best way to do something, and the language should do it that way. This results in a language strict on layout and indentation and even the amount of whitespace to use (!), which of course feels stifling to Ruby proponents. However, this regimented philosophy results in Python being supremely readable and easy to learn – in fact a good number of schools and colleges use Python as a teaching aid. Its syntax is very simple, there is little to remember, and it is thus great for beginners.
Python also boasts extensive libraries and an OO nature, though this is not as ‘pure’ as Ruby’s and is similar to object orientation in C++. Some more features in which Python is superior to Ruby are listed below and also expanded on here.
- Better namespace handling and use of modules
- Central use of iterators
- Internal functions
- A richer set of data structures
Web Apps: Ruby Has The Edge
Web development remains one of the chief arenas in which Ruby and Python compete. Sure, there is server scripting, enterprise apps, mobile apps, and now machine learning, but the traditional user of these languages was the web developer. In the web applications arena, for better or for worse, Ruby has been the much more dominant player, thanks to Ruby On Rails. Ruby’s flexibility, fully promoted by its philosophy that allows multiple ways of getting the same thing accomplished, is a boon to web developers. It’s tempting to think that one simple way of doing things will work for everyone, but when the use cases that web developers diverge as widely as they do, it’s only practical to let multiple approaches flourish. In this case, the success of Ruby is reminiscent of that of C++, a much-maligned language that has succeeded so widely because it lets any team pick whichever particular subset of the language they want to use.
Ruby On Rails is like Ruby on steroids, complete with extensive behind the scenes code generation and server-side metaprogramming that would make coders in a more staid language community raise eyebrows. Indeed this was the case when Django, over in Python land, emulated the Rails MVC approach and immediately faced pushback from Pythonistas who thought the framework was doing too much and getting too complex, out of line with the Pythonic emphasis on readability. Where Python programmers, and Django, went with simplicity, Ruby On Rails went with power and flexibility. True, it can get complex, but the flexibility has proven valuable for web developers. This has led to Rails being almost a rite of passage for “cutting edge” startups especially those developing B2C platforms, with a plethora of big successes such as Airbnb, Twitter, Github, and others too many to count. Rails development is insanely fast, hyper productive, and allows an early stage startup to iterate flexibly with next to no technical staff beyond a couple of core ROR developers. Ruby has also done well in devops, with tools like Chef taking full advantage of the expressive power of the scripting language.
All this does not mean that Python is a slouch in web development. It has some unique tricks all its own: speed and a super large ecosystem. There is a perception that Python is faster than Ruby, and this has often led teams to prefer it over Ruby for web development. The Ruby community is painfully aware of this, and Ruby has gotten way faster over the years. Now, in benchmarks, Ruby performs just about as well as Python, if not better. Still, that’s not enough for the Ruby community, whose Ruby Version 3 was specifically planned to be much faster than Ruby 2, and, pointedly, aims to be much faster than Python3. Either way, the battle is on.
Ruby vs. Python: Open Source Community
In numbers, Python has a stunning edge, but that’s to be expected given how versatile Python is as a general-purpose programming language. Its simplicity and beginner-friendly semantics has seen it make deep inroads into just about any area of computing imaginable. The numbers speak for themselves. Python has an estimated 8.2 million developers around the world, nearly four times the estimated 1.8 million developers that Ruby has. Python’s community dwarfs Ruby in this respect.
However, it’s good to be aware of how passionate the Ruby community is. This counts for a lot more than it would seem on the surface. In technical circles, the mere size of a community can be dwarfed by the passion of the community. Languages with unique features that are valued by their niche communities can have very passionate devotees.The functional family of languages like Haskell, O’Caml, and Elixir are good examples. They have tiny communities, and have had for years, but these languages have not disappeared, or their communities grown disenchanted with them. Rather, whether at Wall Street banks doing esoteric derivatives computations or in academia, their adherents have dug in even deeper and there is a lot of dynamism in these much smaller communities. Ruby’s community looks a lot like them in this respect.
Still, the numbers matter, and if you want the largest, most diverse kind of community spanning scientific computing, game development, web development, and everything else, Python beats Ruby hands down. On Github, Python has over 29,000 stars, 13,000 forks, and 1,100 repository watchers. For Ruby, these figures are 16,500 stars, 4,400 forks, and 1,200 watchers. Even more important, Python has 1,133 contributors to Ruby’s 182. Again, Ruby’s smaller community is very tight knit and super passionate so you dismiss them at your own peril. Python’s community benefits from the cross-pollination of different areas of technical focus, and allows greater diversity of job roles and areas in which the language is applied. Quite naturally, Python has a huge lead on Ruby in general developer interest broadly defined, as well as jobs, teams using it, and other usage stats.
Machine Learning: Python Has The Edge
Data science and machine learning are just coming into their own, but, the world is now realizing the portent power of data science, spanning all the way from big data to artificial intelligence (AI), and deep learning. Data science, and machine learning in particular, allows machines to make human-like decisions and “learn” from data to get better over time at solving a dizzyingly wide assortment of problems. A lot of value has been unlocked and, naturally, businesses from tech giants like Google to small scrappy startups, are gearing up and making strides in this lucrative field. This has proven to be Python’s most portent area of accomplishment.
A machine learning comparison is a nightmare scenario for just about any language when matched up against Python. Indeed, in data science circles, especially those in academia, the comparison between Ruby and Python in this sphere would not warrant much beyond a cursory dismissal of Ruby. If Ruby shines in web development, Python owns the data science and machine learning fields. Ruby is not even among the top 5 programming languages for machine learning. Python heads the billing here, with the top 5 machine languages as follows:
- Python - top spot with 57% of data scientists using it, other estimates in the industry place it much closer to 70%.
- C++ - holds second spot with 44% of data scientists using it in some capacity
- Java - third spot
- R - fourth place
- JavaScript - 5th place, with 31% of data scientists using the language
The top 3 languages in machine learning, Python, C++, and Java, account for the lion’s share of all serious machine learning, and they are used by slightly different concentrations of data scientists, with plenty of overlap among them. Other important machine learning languages in the top 10 are Julia, Scala, Ruby, Octave, and MATLAB. However, these languages have less than 5% uptake among data scientists each. This is not to say these languages are unfit for machine learning. That’d be almost incontrovertibly outlandish a notion, as Julia or Octave data scientists would point out, when these languages, even more so than Python, are purpose-built to solve hard core data science computations. Python, however, had an early lead in data science, a huge community, incredible general-purpose compatibility, simplicity, and workability on large teams. These factors have made it especially hard to dislodge in machine learning. It goes without saying that if you and your team are going to be doing heavy duty machine learning, Python should be the heavy favorite over Ruby.
Ruby vs. Python: Frameworks And Libraries
As open source programming languages with particularly vibrant communities, both Ruby and Python have stunningly complete ecosystems, with libraries that showcase the power of the open source ethos. Just about anything you can imagine can be accomplished with freely available open source packages in either programming language. For Ruby, programs are largely distributed via RubyGems, and for Python via the Python Package Index (PyPI).
Other software distribution systems to complement these exist, and there’s excellent tooling on both sides. This is a commonality between Ruby and Python that makes the communities, so different in many ways, much closer in their approach to open source.
Here’s a look at some of the top libraries in the Ruby ecosystem:
- Web App Frameworks - rails, sinatra, rack
- Web servers - puma, unicorn
- Database adapters - pg, mysql2
- Ruby core extensions - activesupport, hashie
- Concurrent processing - eventmachine, concurrent-ruby
- Testing tools - rspec, capybara, minitest
For Python, a who’s who of top libraries showcases the broader areas of focus where the Python programming language has made serious inroads, with a pronounced focus on areas related to data science:
- Django (web framework)
- Numpy (data science)
- Pandas (data science)
- Matplotlib (data science)
- Scipy (data science)
- Flask (web framework)
- Keras (machine learning, deep learning)
- Theano (data science)
- TensorFlow (machine learning, neural networks)
- Scikit-learn (machine learning)
- PyQT (graphical user interface, GUI development)
- wxPython (GUI development)
- Pygame (video game development)
- Tkinter (GUI package)
Python’s vast data science footprint continues to grow, and its data science libraries, in many ways the leading libraries in that respect, are growing in user adoption among Python developers.
Ruby Vs Python: Code Side-By-Side
Ruby code and Python code has a lot in common, but there are subtler differences, especially in coding idioms driven largely by philosophical differences. Both are dynamic, interpreted languages. Both use white space and don’t have the braces made popular by C, C++, and Java, among other languages. These languages are truly versatile, however, as revealed by the paradigms they support: procedural, imperative, functional, and object-oriented. Yes, certain super functional-style code is far easier to write in Ruby than in Python, but Python, despite its drive for simplicity, can be used in ways rather similar to Ruby. Ruby, however, tends to be more expressive, and strikes a bit closer to functional languages like Lisp or Scheme than Python. Syntactically, and in many other ways, Ruby code looks a lot more like Python.
Here is a simple example that illustrates how close these two really are, while being far from the clones they might look like on the surface:
Fibonacci function in Ruby
def fib(n)
n < 2 ? n : fib(n-1) + fib(n-2)
end
alias :fibonacci :fib
Fibonacci function in Python
def fib(n):
if n < 2:
return n
else:
return fib(n-1) + fib(n-2)
The Python code is just a tad simpler, while the Ruby code prefers power over simplicity. Still, both of these are much closer than would be examples in Java, C++, and other languages. What this means is that if your team ever has to switch language, moving from Ruby to Python or vice versa, while often considered inconceivable by deep enthusiasts in either software development camp, the sky would not exactly fall down. The learning curve is much easier to scale for developers and teams moving from one to the other, than it would be to move to a language such as C, C#, or even Erlang.
Conclusion
Despite the noisy arguments from both languages’ camps, it is impossible to say whether one language is overall ‘better’ than the other. It’s clear that each has some areas it is better suited to, because of its features and support from other users in the same area. For Ruby this is web development via the Rails framework, and for Python it is scientific and academic programming. And each has some features or capabilities that the other does not have or does not do well.
The two languages also espouse radically different philosophies. Ruby focusses on giving developers the freedom to do whatever they want and staying out of their way. Python insists on ease of learning and use by zeroing on only on the one right way to do something. This produces an interesting culture split between the camps – Python developers are somewhat conservative and value stability over change, developments and new features are added slowly. Ruby adherents seem to thrive on change and freedom. For instance the Rails framework is constantly changing, and in fact many of the changes and new features in Python are first tested in Ruby. Read more about these different mindsets here.
Ruby vs Python Infographic
References
- https://www.columnfivemedia.com/work-items/infographic-code-wars-php-vs-ruby-vs-python-who-reigns-supreme
- http://dpk.io/pyvsrb
- https://bitboxer.de/2012/10/03/ruby-vs-python/
- http://www.senktec.com/2013/06/ruby-vs-python/
- https://stackoverflow.com/questions/1113611/what-does-ruby-have-that-python-doesnt-and-vice-versa
- https://wit.io/posts/ruby-is-beautiful-but-im-moving-to-python