• Stars
    star
    317
  • Rank 132,216 (Top 3 %)
  • Language
    Python
  • Created over 10 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Should I Learn Python or Ruby?

Should I Learn Python or Ruby?

pyvsrb

At first glance, Python and Ruby appear to be very similar languages. Both are high-level, dynamic languages used for rapid development. Both are beautiful languages that, when written well, are intuitive and can read much like English.

What do I mean by dynamic? Well, with a dynamically typed language you can do this:

>>> variable = 1
>>> type(variable)
<class 'int'>
>>> variable = "Foo"
>>> type(variable)
<class 'str'>
>>> variable = ["bar",10]
>>> type(variable)
<class 'list'>

Essentially, you can change the datatype (from an integer to a string to a list, in the above example) at any point in a program. In a statically typed language, this would result in an error when compiled.

Both support multiple programming paradigms as well - e.g, procedural, functional, object oriented, and so forth.

However, there are a number of differences ..

This article begins with the language structure, then moves toward detailing what you can do with the two languages. Although the differences are important, it's much more important to focus on your end goal - that is, what you want to accomplish with said language. More on this later.

The Python Way

Open your terminal and enter the Python shell then type import this:

>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

This is what is known as the Zen of Python, which are the guiding principles of Python. These 19 guidelines can be trimmed down to five points:

  1. Beautiful is better than ugly.
  2. Explicit is better than implicit.
  3. Simple is better than complex.
  4. Complex is better than complicated.
  5. Readability counts.

Essentially, Pythonists value code readability and productivity over all else.

Check out this Slideshare to see examples of all the guidelines in use. Highly recommended!

Finally, if shortened to one major point-

There should be one – and preferably only one – obvious way to do it

-we get to one of the main differences between Python and Ruby ..

The Ruby Way

While Python values one main way of solving a problem, Ruby - influenced by Perl - provides the developer with more freedom and power:

Ruby inherited the Perl philosophy of having more than one way to do the same thing. I inherited that philosophy from Larry Wall, who is my hero actually. I want to make Ruby users free. I want to give them the freedom to choose. People are different. People choose different criteria. But if there is a better way among many alternatives, I want to encourage that way by making it comfortable. So that’s what I’ve tried to do. Maybe Python code is a bit more readable. Everyone can write the same style of Python code, so it can be easier to read, maybe. But the difference from one person to the next is so big, providing only one way is little help even if you’re using Python, I think. I’d rather provide many ways if it’s possible, but encourage or guide users to choose a better way if it’s possible.

—Yukihiro Matsumoto (Matz)

Source: http://www.artima.com/intv/rubyP.html

With more freedom and less syntactical rules, many Rubyists believe that Ruby is a much more elegant language - and it is. Also, while Python focuses on simplicity, Ruby focuses on making the programmer happy, while this tends to be true and people can make truly beautiful programs, you can also often see messy code (especially from beginners) that can be difficult for other developers to read( or even themselves in a not so distant future). For example, you can put multiple statements on one line. This can look good and be readable, depending on how it's coded - or it can be a mess.

The principle of least astonishment

One principle very present in the design of Ruby is the principle of least astonishment - the language working the way you think it should.

In order to behave like this Ruby tends to do things like having the same functions with different names, which can be confusing for beginners. Although Python does not have this principle in mind when making the language, it also behaves quite like this by make pretty much everything work in a standard way. One could argue that this only benefits advanced Python users, though.

Let's compare some code. The following snippets of code are for solving the Fibonacci sequence:

Ruby:

def fib(n)
    n < 2 ? n : fib(n-1) + fib(n-2)
end
alias :fibonacci :fib

Python:

def fib(n):
    if n < 2:
        return n
    else:
        return fib(n-1) + fib(n-2)

Although you can write this code in many ways, both of these methods are true to the language.

The Ruby code demonstrates the practices of using ternary operators and of having multiple names for the same function (fib and fibonacci are the same).

In other words, the Ruby example is very Ruby-ish while the Python example is very Pythonic. Can you read the Ruby code? It may be more elegant but it's a bit harder to read. Meanwhile, it's easy to follow the Python code, right? You of course can write code anyway you want. It's advisable to write Ruby code, when beginning, in a more Pythonic way - which simply means making it more readable:

def fib(n)
  if n < 2
      n
  else
    fib(n-1) + fib(n-2)
  end
end

Keep in mind that in many cases with Python there are still a number of ways to do one thing. Take copying a list for example. There are at least four different ways:

>>> my_list = [1,2,3]
>>> my_new_list = my_list[:]
>>> my_new_list
[1, 2, 3]
>>> my_new_list = list(my_list)
>>> my_new_list
[1, 2, 3]
>>> from copy import copy
>>> my_new_list = copy(my_list)
>>> my_new_list
[1, 2, 3]
>>> my_new_list = [x for x in my_list]
>>> my_new_list
[1, 2, 3]

The difference is that there is one right way of doing this given the situation. The latter two probably are not the best to do since you have to use an extra library and list comprehensions can often be hard to read, respectively. The second example is the most readable, so that should probably be used in most situations.

More differences

As you can imagine, there are many more differences than just the syntax and philosophies of the two languages. Let's quickly look at some examples.

Learning Curve

Without a doubt, Python is much easier to learn because of how the language is structured - and how explicit it is. One can literally become proficient in two to three months. Ruby takes much longer to learn due to its flexibility. Beneath the elegant surface, there's a lot of magic happening. It takes a while to grasp exactly what is happening. It can take upwards of six months to become proficient in Ruby.

You can see just how explicit Python is based on this example:

from twitter import Twitter

twit = Twitter()
require 'twitter'

twit = Twitter.new

In the first example (Python), we are importing the Twitter() class from the twitter library, while in the latter example (Ruby), we are simply importing the twitter library, giving us access to the entire library, not just the Twitter() class. So you can see that in Python, you import only what you need, nothing else.

learning_curve

Programming Paradigms

Again, you can use the same paradigms in both languages (procedural, functional, object oriented ...). When it comes to object oriented programming, Ruby used to have the upper hand, as it was built specifically for object orientation. That said, Python has moved more towards being a true object orientated language over the last few years. However, Ruby has one advantage: it can add methods to existing classes, while Python by default can't do this (although it's possible with the use of external libraries).

Performance

Performance is a toss up as well. In some cases Python performs better, while in others Ruby outperforms Python. It all depends on the task at hand.

Usage

Ruby has a bigger web presence with Rails than Python does with Django, so if you're looking to move into web development, Ruby may be the way to go. Python is a great general-purpose language and has more momentum going for it for areas outside of the web, such as sys admin/DevOps, statistics, and scientific computation.

That said, take a look at the two code snippets below -

print("Hello, World!")

and

puts 'Hello, world!'

End users do not care about the syntactical differences; they just want to see "Hello, World!" outputted on their screen. Think about this site that you're reading this post on now. Do you really care of it's Ruby/Rails-based or Python/Django-based? I imagine you just want to be able to read this post. So, if you want to go into web development, worry less about the back-end language. Learn one, then get really good at. Then go learn JavaScript.

Community

The Python community is active, vibrant, and truly helpful. Although you can say the same about the Ruby community, the community itself is very much tied into Rails. If Rails is your thing, then you are in luck.

Popularity/Jobs

popularity

Source: http://blog.codeeval.com/codeevalblog/2016/2/2/most-popular-coding-languages-of-2016

popularity

For the fourth year in a row, Python is the most popular language. Also, notice how Ruby decreased in popularity:

popularity

Source: http://blog.codeeval.com/codeevalblog/2014#.Uxewd_SwL5g

Excellent article about jobs by programming language.

Indeed:

Average Salary

  1. Rails Developer - SF
  2. Django Developer - SF

Companies

  1. Python - Disqus, Dropbox, Yelp, Google, Reddit
  2. Ruby - Hulu, Twitter, Github, Airbnb

Extended Code Example (for further comparison)

Guessing game ...

Python

import random


number = random.randint(1, 20)
guesses = 0

print('Hello! What is your name?')
name = input()

print(f"Hi, {name}. I'm thinking of a number from 1 and 20.")

while guesses < 6:
    print(f'What is your guess? You have {6 - guesses} more guesses.')
    guess = input()
    guess = int(guess)

    guesses = guesses + 1

    if guess < number:
        print('Too low.')
    elif guess > number:
        print('Too high.')
    elif guess == number:
        print(f'Good job, {name}! You guessed my number in {guesses} guesses!')
        break

if guess != number:
    print(f'Nope. The number I was thinking of was {number}.')

Ruby

number = rand(1..20)

puts 'Hello! What is your name?'
name = gets&.chomp

puts "Hi, #{name}. I'm thinking of a number between 1 and 20."

1.upto 6 do |guesses|
  puts "What is your guess? You have #{7 - guesses} more guesses."
  guess = gets&.chomp.to_i

  if guess == number
    puts "Good job, #{name}! You guessed my number in #{guesses} guesses."
    exit
  else
    puts(guess > number ? 'Too high' : 'Too low')
  end
end

puts "Nope. The number I was thinking of was #{number}."

What are the syntactical differences? Is the Python code Pythonic? Is the Ruby code Ruby-ish? Do either (or both) need to be refactored?

Conclusion

As you can tell, there's not too many differences in the languages themselves. Yes, Python values readability, one method of doing things, and being explicit - but these are minor differences. It's more about the end goal. What are you trying to accomplish? My advice: Try both languages. See which language you like better, then learn that one. After that, learn the other.

Always, always, ALWAYS remember that no language can do it all. In other words, all languages have their positives and negatives

Cheers!

Resources

  1. Python vs Ruby
  2. Why I push for Python
  3. 4 Reasons Why You Should Learn Ruby As Your First Programming Language
  4. How do Python and Ruby compare?
  5. Why Python is a Great First Language
  6. Python vs Ruby
  7. Ruby on Rails vs Python and Django: Which Should a Beginner Learn?
  8. Python Environment Management for Rubyists – a Guide
  9. Ruby vs Python
  10. Ruby vs Python, the Definitive FAQ

More Repositories

1

awesome-fastapi

A curated list of awesome things related to FastAPI
5,592
star
2

flaskr-tdd

Flaskr: Intro to Flask, Test-Driven Development (TDD), and JavaScript
Python
2,259
star
3

awesome-flask

A curated list of awesome things related to Flask
1,143
star
4

cypress-visual-regression

Module for adding visual regression testing to Cypress
JavaScript
483
star
5

passport-local-express4

check out the blog post
JavaScript
290
star
6

node-stripe-charge

node + stripe + express + bootstrap 4 (used for one time charges)
JavaScript
272
star
7

microservice-movies

JavaScript
248
star
8

sublime-setup-for-python

setting up sublime text for python development
JavaScript
241
star
9

flask-redis-queue

Example of how to handle background processes with Flask, Redis Queue, and Docker
Python
207
star
10

Scrapy-Samples

Scrapy examples crawling Craigslist
Python
196
star
11

thinkful-html

intro to html and css
HTML
163
star
12

thinkful-mentor

code samples, refactoring - for thinkful students
TSQL
158
star
13

thinkful-angular

AngularJS by Example - Building a Bitcoin Investment Calculator
JavaScript
130
star
14

node-koa-api

JavaScript
130
star
15

flask-tracking

http://www.realpython.com
Python
129
star
16

typescript-node-api

TypeScript
128
star
17

node-postgres-promises

JavaScript
104
star
18

flask-basic-registration

Basic user registration package for Flask.
Python
102
star
19

passport-examples

social auth for node
JavaScript
93
star
20

node-postgres-todo

JavaScript
93
star
21

Flask-Landing

simple landing page to collect prelaunch emails
Python
64
star
22

node-swagger-api

JavaScript
63
star
23

node-mocha-chai-tutorial

JavaScript
62
star
24

whiterspace

Another minimal theme for Octopress.
JavaScript
62
star
25

angular-gulp-browserify-seed

Angular starter featuring Gulp, Bower, and Browserify
JavaScript
58
star
26

node-docker-api

developing and testing microservices with docker
JavaScript
57
star
27

twitter-sentiment-analysis

Video tutorial and accompanying output for conducting text sentiment analysis in Twitter
R
56
star
28

mean-auth

User Auth with the MEAN Stack
JavaScript
54
star
29

testing-in-django

Example project for testing in Django.
JavaScript
53
star
30

efk-kubernetes

logging in kubernetes
JavaScript
52
star
31

node-postgres-sequelize

JavaScript
52
star
32

passport-local-knex

adding passport to a node app
JavaScript
51
star
33

angular-auth-ngrx

TypeScript
50
star
34

passport-social-auth

JavaScript
49
star
35

node-docker-workflow

http://mherman.org/blog/2015/03/06/node-with-docker-continuous-integration-and-delivery
JavaScript
48
star
36

passport-local

user authentication to Node.js with Passport.js.
JavaScript
43
star
37

node-express-ajax-craigslist

scraping craigslist
JavaScript
38
star
38

web2py

Intro to web2py
Python
38
star
39

sinatra-blog

Sinatra + PostgreSQL + Heroku - on the center stage
Ruby
38
star
40

flask-stripe

Flask + Stripe + User Registration
Python
34
star
41

flask-spark-docker

Just a boilerplate for PySpark and Flask
Python
33
star
42

python-decorators

into to python decorators
Python
33
star
43

java-for-javascript

Java for JavaScript Developers
Java
30
star
44

node-twitter-sentiment

sentiment analysis with node, express, and the twitter api
JavaScript
30
star
45

mocha-chai-knex

JavaScript
29
star
46

node-token-auth

jwt auth!
JavaScript
29
star
47

angular-testing-tutorial

welcome!
JavaScript
27
star
48

flask-intro

Introduction to Flask showing much of the basic functionality. Plus, I built a task manager application called FlaskTaskr.
Python
26
star
49

node-stripe-example

JavaScript
25
star
50

react-intro

just a basic react intro
JavaScript
24
star
51

flask-rethink

a todo list with flask and rethinkdb
Python
23
star
52

django-stripe

Django + Stripe + User Registration
Python
22
star
53

express-testing-mocha-knex

testing an express app
JavaScript
21
star
54

programming-exercises

Michael Herman's stash of programming exercises
JavaScript
20
star
55

koa-passport-example

koa + passport
JavaScript
17
star
56

noLogo

minimalist octopress theme.
JavaScript
16
star
57

node-grpc-crud

JavaScript
15
star
58

python-devtest

new developer test
Python
14
star
59

vue-intro

intro to vuejs
HTML
14
star
60

flow-node-api

JavaScript
14
star
61

node-getting-started

a little more than hello world!
JavaScript
14
star
62

pinky-promise

why sign an nda when you can just pinky swear instead?
CSS
12
star
63

node-workshop

learn javascript and node!
JavaScript
12
star
64

craigslist-housing-scraper

use sparingly, at your own risk
Python
12
star
65

ancestry-scraper

Python
12
star
66

twitter-sentiment-python

basic sentiment analysis in python
11
star
67

bootstrap3

getting started with bootstrap 3
CSS
11
star
68

flask-songs-api

Python
11
star
69

octoplate

Octopress theme based on Twitter Bootstrap 3
JavaScript
10
star
70

testcafe-example

JavaScript
10
star
71

flask-ajax-form

Generate a dynamic form in Flask with AJAX
JavaScript
9
star
72

TDD-Django

Blog post and associated code repo describing TDD with Django and PyVows.
Python
9
star
73

docker-workshop

JavaScript
8
star
74

django-1.5-template

django 1.5 + custom registration + bootstrap + heroku
CSS
8
star
75

testcafe-visual-regression

JavaScript
8
star
76

running-mate

simple MLOps tool for DS/ML teams
Python
8
star
77

flask-geolocation

that's right: flask and geolocation
Python
8
star
78

django-tutorial

Code from a brief tutorial showing the basics of installing Django and setting up a simple app.
Python
8
star
79

django-tastypie-tutorial

Super Basic REST API with django-tastypie
Python
8
star
80

node-express-swig-mongo

JavaScript
8
star
81

flask-scrapy-test

for kristjan
Python
7
star
82

bottle-plotly-python

bottle.py and the plot.ly API
Smarty
7
star
83

node-bootstrap3-template

node, express, bootstrap3, boilerplate, starter, template, mongo-ready
JavaScript
7
star
84

microservice-ping-pong

JavaScript
6
star
85

bottle-user-authentication

bottle.py user login/logout/authentication bootstrap3
Python
6
star
86

django-tdd

django 1.6 test driven development!
Python
6
star
87

express-crud-pg-promise

Just a basic Node + Express CRUD app with pg-promise
JavaScript
5
star
88

hibbert-django

mike hibbert
JavaScript
5
star
89

node-oauth-openid

JavaScript
5
star
90

node-request-cheerio-scraper

scraping HN => CSV
JavaScript
5
star
91

angular4-crud

TypeScript
5
star
92

job-scraper

indeed
Python
5
star
93

generator-herman-express

generator for a basic Node/Express boilerplate
JavaScript
5
star
94

docker-rails-ecs

deliver rails app to ecs
Ruby
5
star
95

mean-token-auth

JavaScript
5
star
96

textbook

learning management system
JavaScript
5
star
97

mjhea0.github.com

code for my blog, powered by github pages and jekyll
HTML
5
star
98

ultimate-craigslist-scraper

get jobs. lots of 'em.
Python
5
star
99

indeed-scraper

stay away
Python
4
star
100

web2py-heroku

heroku + web2py
4
star