What's the difference? Ruby vs. Python

Megan - Jan 2 '20 - - Dev Community

After spending a month and a half working exclusively in Ruby and Rails, I feel that I have a pretty solid knowledge base for the language. But Python is a very in demand language at the moment, and I've heard time and time again that the languages are very similar and both easy to use. This really sparked my curiosity, and originally I had decided that after my coding bootcamp I would try my hand at Python see what all of the hype is about. But coming fresh off of working in Ruby, I thought it'd be a perfect time to compare and contrast the two both logistically and technically to give anyone an idea if you're looking into using one or both of these languages.

Both Ruby and Python are dynamic, object oriented programming languages that can be used for a multitude of purposes, from building web applications to hosting and accessing databases.

Ruby

  • Made to be easily read and understandable in a way similar to English
  • Multiple ways in which something can be written, allowing for user customization and personalization
  • Used mostly for free and flexible web development
  • Most popular framework is Rails
  • Examples of websites made using Ruby include Airbnb, Hulu, ZenDesk, GitHub, Basecamp, Kickstarter

Python

  • Very strict way in which something can be written, one general formula for achieving things
  • Used substantially for data science and scientific programming
  • Most popular framework is Django
  • Examples of websites made using Python include Spotify, Youtube, Dropbox, Reddit, Instagram

Now to dip our toes into the technical differences, we can start with an oldie but a goodie, our classic hello world example.

#Python
message = "Hello World!"
print(message)
Enter fullscreen mode Exit fullscreen mode
#Ruby
message = "Hello World!"
puts(message)
Enter fullscreen mode Exit fullscreen mode

For simply logging to the console, Ruby uses puts whereas Python uses print. Both Ruby and Python execute code from the top down, so anything else we add to this snippet will be printed to the terminal after our original "Hello World!" line. It will also overwrite anything that was previously declared, and the last line of code will be the final output.

But they both don't differ in the way that variables are declared or assigned. It is as simple as your variable name set equal to its value. They do differ slightly in terms of data types as you can compare and contrast from the table below:

Data Type Ruby Python
integer
float
complex
boolean ✓(true or false) ✓(True or False)
string
undefined nil None

In Ruby, generally a collection of data can be displayed in either an array or a hash. In Python, data is stored in a similar way but denoted as either a list or a dictionary.

#Ruby array
nums = [1, 2, 3, 4, 5]
puts nums[1] #2
Enter fullscreen mode Exit fullscreen mode
#Python list
nums = [1, 2, 3, 4, 5]
print nums[1] #2
Enter fullscreen mode Exit fullscreen mode

When accessing items from an array or list, Ruby and Python are generally the same as denoted above. But when taking a range of items, they differ only by the symbol used. Ruby uses ... and Python :. Python's : tends to be exclusive, whereas Ruby's ... tend to be inclusive.

nums = [1, 2, 3, 4, 5]

puts nums[0..3] #1, 2, 3, 4
#prints from index 0 to 3, inclusive
Enter fullscreen mode Exit fullscreen mode
#Python
nums = [1, 2, 3, 4, 5]

print nums[0:3] #1, 2, 3
#prints from index 0 to 2, exclusive

print nums[1:] #2, 3, 4, 5
#will print from index 1 to end

print nums[:4] #1, 2, 3, 4
#will print from first(index 0) to index 3
Enter fullscreen mode Exit fullscreen mode

As you can tell so far, these languages share remarkable similarities. The only noticeable difference in terms of Ruby hashes and Python dictionaries is that Ruby allows for a => to be used when assigning a key its value, and Python strictly uses colons. Ruby can also use colons for value assignment, but varies slightly in the way that the value is accessed.

#Ruby hash 
colors = { 1 => 'red', 2 => 'green', 3 => 'blue'}
puts(colors[1]) #red
Enter fullscreen mode Exit fullscreen mode
#Python dictionary
colors = {1 : 'red', 2 : 'green', 3 : 'blue' }
print(colors[1]) #red
Enter fullscreen mode Exit fullscreen mode

In addition to lists, Python also has a few other data collection types known as tuple and set.

Unlike lists, tuples are denoted by parentheses and are ordered and unchangeable. In order to add or remove items, the tuple would need to be changed into a list and then back into a tuple. Data inside of a tuple is accessed the same way as a list.

#Python tuple
teas = ("black", "green", "chamomile")
print(teas[-1]) #chamomile
Enter fullscreen mode Exit fullscreen mode

The last one is a set, which is a collection that is denoted by curly braces but does not have an order or index. That means it is not possible to access a value using an index, but it can be looped through to see all of the values. Values can also be added to a set.

flowers = {"lily", "azalea", "buttercup"}
print(flowers) #azalea, lily, buttercup
Enter fullscreen mode Exit fullscreen mode

(note: the order will be random when printed to the console)

Looping

For and while loops are very, very similar in Ruby and Python. The only noticeable difference is that Python uses a colon to denote when the loop is to begin and Ruby has to end each loop with an end.

#Ruby
nums = [1, 2, 3, 4, 5]

for i in nums do
  puts i
end
#1, 2, 3, 4, 5

j = 0
while j < 7
  puts j
  j += 1
end
#1, 2, 3, 4, 5, 6
Enter fullscreen mode Exit fullscreen mode
fruits = ["apple", "banana", "cherry"]
for i in fruits:
  print(i)
#apple, banana, cherry

j = 1
while j < 6:
  print(j)
  j += 1
#1, 2, 3, 4, 5
Enter fullscreen mode Exit fullscreen mode

They have a few special cases, as Ruby has until and Python has continue. But these don't readily affect their general uses for for and while loops.

If statements

These two are also very similar in Ruby and Python. Differing only by Python using a : to denote the start of the statement and their interesting spellings for else if.

i = 2
if i <= 3
   puts "aww, what a small number."
elsif i > 3 and i < 10
   puts "that's a medium sized number"
else
   puts "wow, that's a big number!"
end
#aww, what a small number.
Enter fullscreen mode Exit fullscreen mode
c = 5
d = 1
if c > d:
  print("c is greater than d")
elif c == d:
  print("c and d are equal")
else:
  print("c is greater than d")
#c is greater than d
Enter fullscreen mode Exit fullscreen mode

Interpolation and String Literals

Both languages use curly braces {} to interpolate into strings. But Ruby requires a # before the curly braces and Python does not.
Also, Ruby uses single or double quotes to span over multiple lines (double quotes are required for interpolation) and Python uses triple quotes.

name  = "Leslie"
occupation = "park director"
"Hello, my name is #{name} & I'm a(n) #{occupation}."
Enter fullscreen mode Exit fullscreen mode
pet = 'dog'
age = '6'
print(f'''I have a {pet} who is {age} years old.
{pet}s are very fun!''')
#I have a dog who is 6 years old. dogs are very fun.
Enter fullscreen mode Exit fullscreen mode

Python3 requires a f for a formatted string, meaning a string that will include interpolation into it.

Lastly, there are also a few other small things I noticed when comparing these two languages:

  • print requires parentheses to function, whereas puts does not
  • # works for commenting out in both Ruby and Python

If you're new to Python like me, have been interested in Ruby and wanted to see the differences, or just come to learn a bit about both, I hope this has been helpful and maybe given you a little insight into two of the most popular and dynamic programming languages today.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player