Ruby is a general-purpose, dynamic, open source programming language that focuses on simplicity and productivity. The Ruby programming language is frequently compared to Python for their similarities. Many developers say that learning Ruby is easier if you know Python due to their similarities. However, these two languages differ in major ways.
We’ve created this basic introduction to help you make the transition from Python to Ruby. We’ll go over their main differences and then take a dive into Ruby’s syntax so you can get hands-on with Ruby code quickly.
Here’s what we’ll cover today:
Start building with Ruby code
This introductory course offers tons of hands-on practice with interactive illustrations exercises for all the fundamentals you need to know to be working with Ruby in no time.
Ruby and Python are both popular programming languages known for their speed, simplicity, and user-friendly syntax. Both languages are flexible, object-oriented , dynamic, and have a lot of useful frameworks and libraries to use, particularly for web applications and web development (Ruby on Rails for Ruby or Django for Python).
Ruby is a high-level, interpreted programming language, created in 1995 by Yukihiro Matsumoto. His goal was to make an object-oriented scripting language that improved on other scripting languages of the time.
Ruby also allows for functional programming and is known for being very similar to written human languages, making it easy to learn.
Python is an interpreted, high-level general-purpose language, created by Guido van Rossum with the goal of code readability. Python is know well-loved for its robust standard library.
Ruby | Python |
---|---|
No primitive data types. Everything is an object. | Has primitive types and objects |
Mixins can be used | Mixins cannot be used |
Less available functions. Relies mostly on methods. | Lots of available functions |
Cannot modify built-in classes | Can modify built-in classes |
Supports Tuples with Rinda. Other collections include Arrays, Hash, Set, and Struct. | Supports Tuples, Sets, Dictionary (Hash), and Lists |
Iterators less common | Iterators are very common |
Both languages are used by big companies. Companies that use Python include YouTube, Instagram, Spotify, Reddit, Dropbox, while Ruby is used at Hulu, Basecamp, GitHub, and Airbnb. Ruby developers also tend to make higher salaries than Python developers. StackOverflow’s 2020 survey lists Ruby’s global average salary at $71k and Python’s at $59k.
Both Ruby and Python offer higher annual salaries than other software development languages, including PHP, JavaScript, and Java.
Python is currently the go-to language for machine learning (ML) and artificial intelligence (AI) due to its extensive libraries and visualization tools. Ruby does offer some competitive options, but there is a long way to go before it will hold a torch to Python. So, if you are looking to work in data science, Python is the winner.
The main web frameworks for both Ruby and Python, Django and Ruby on Rails, are rather similar. Both are based on the classic model-view-controller (MVC) pattern, and they both provide similar repositories (PyPi for Python and RubyGems for Ruby). Both frameworks perform well and are easy to learn.
Test-driven development (TDD) is pretty standard for both Ruby and Python, but Ruby does offer behavior-driven development (BDD) while Python does not, which may be useful in some cases
Both languages have large, active communities, which Python’s community being a bit larger, namely because Ruby is most popular for its development tool Ruby on Rails. Both communities seem to be equally active and supportive.
One benefit to Ruby is that there are unique Ruby forums and job boards since the language is more specialized in some regards.
Now that we understand the difference between Ruby and Python at a high level, let’s dive into the main code differences between the two. We’ve compiled the main things that differ from Python below.
In the Ruby programming language:
false
and nil
evaluate to false, and everything else is true
elsif
instead of elif
require
instead of import
yield
executes another function that is passed as the final argument, then it resumespublic
, private
, and protected
for accessNow that we understand how Ruby and Python differ at the code level, let’s look at some actual Ruby code, starting with the classic Hello World program. Take a look below and note how simply Ruby’s syntax is here.
puts "Hello World!"
Here, the puts
keyword is used to print. Remember: Ruby’s code is very readable, designed to emulate spoken English language.
There’s even a simpler way to do this. Ruby comes with a built-in program that will show the results of any statements you feed it, called Interactive Ruby (IRB). This is the best way to learn Ruby. First, open IRB:
irb
. Hit enter.irb
, and hit enter.If you type:
"Hello World"
You will get the following:
irb(main):001:0> "Hello World"
=> "Hello World"
The second line tells us the result of the last expression. We can print this using the puts
command we learned before.
irb(main):002:0> puts "Hello World"
Hello World
=> nil
Here, => nil
is the result of the expression, since puts
always returns nil
.
Let’s now quickly go over the basics of Ruby’s syntax that may be different from what you’re used to in Python. Note how Ruby differs and how it is similar as you read.
In Ruby, you assign a name to a variable using the assignment operator =
, like so:
puts number = 1
Here is a list of the different kinds of variables in Ruby:
something
)@something
)Something
or SOMETHING
)$something
)Keywords and identifiers are similar to Python. Identifiers are case sensitive, and they may consist of alphanumeric characters and underscore _
.
Ruby’s reserved keywords include the following:
In Ruby, a string is a sequence of characters inside quotation marks " "
. We can also use single quotation marks.
You can concatenate strings with the plus sign +
.
puts "snow" + "ball"
In Ruby, multiplying a String by a number will repeat a String that many times.
puts "Ruby" * 3
Some important methods for Ruby strings include:
size
empty?
include?
gsub
split
Pro Tip: Ruby’s percentage sign
%
shortcut can be used with strings and arrays
%w
will create an array of strings
%i
with create an array of symbols
%q
will create a string without quotation marks
In Ruby, you can create a Hash by assigning a key to a value with =>
. We separate these key/value pairs with commas and enclose the whole thing with curly braces.
{ "one" => "eins", "two" => "zwei", "three" => "drei" }
This defines a Hash with three key/value pairs, so we can look up three values (the strings "eins"
, "zwei"
, and "drei"
) using three different keys (the strings "one"
, "two"
, and "three"
).
Some important methods for Ruby hashes include:
key?
fetch
merge
new
(for default values)In Ruby, we create an Array by separating values with commas and enclosing this list with square brackets, like so:
[1, 2, 3]
Note: Ruby Arrays always keep their order
Like in Python, there are all sorts of things you can do with Arrays. The most important methods being:
size
empty?
push
/ pop
join
flatten
Here is an example of the intersection operator &
, which finds the intersecting parts of our arrays:
puts ([1, 2, 3] & [2, 3, 4])
In Ruby, parenthesis and semicolons are not required, but we can use them. However, we follow these basic rules:
def foo(a, b, c)
(a.size + b.size) * 2
def foo
There are three main ways we can add comments to a Ruby program.
# Single line comment
# Multiple
# Lines
=begin
Block fashion
Commenting
=end
In Ruby, we define methods using the def
keyword followed by method_name
. IT ends with the end
keyword.
def method_name
# Statement
# Statement
.
.
end
We pass parameters to our methods in parentheses.
def method_name(var1, var2, var3)
# Statement
# Statement
.
.
end
In Ruby, we create classes using the class
keyword followed by the name of the class.
class Class_name
end
Note: The first letter of your class names should be a capital letter.
In Ruby, objects are created with the new
method.
object_name = Class_name.new
Conditional statements are similar to most other languages with a few slight differences. Take a look at the examples below to get at sense of how they look in Ruby.
number = 5if number.between?(1, 10)puts "The number is between 1 and 10"elsif number.between?(11, 20)puts "The number is between 11 and 20"elseputs "The number is bigger than 20"end
Note: The
elsif
andelse
statements are optional. You can have anif
statement withoutelsif
orelse
branches, anif
statement only with anelse
, or you could have anif
statement with one or moreelsif
statements.
Ruby also has a shorthand for working with conditional statements. So, we could write this bit of code:
number = 5
if number.odd?
puts "The number is odd."
end
As this instead:
number = 5
puts "The number is odd." if number.odd?
Now that you know the basics of Ruby and know how it differs from Python, you’re ready to cover more advanced topics. We recommended studying the following concepts next:
To get started with these concepts and get hands-on with Ruby, check out Educative’s course Learn Ruby from Scratch. This introductory course offers tons of hands-on practice on all the need-to-know topics, including variables, built-in classes, objects, conditionals, blocks and much more!
By the end, you’ll be a confident Ruby developer, ready to take on complex projects!
Happy learning!
Free Resources