No, you don't need a basic understanding of Ruby to get started!
New to Ruby and Rails? Here's everything you need to know about Ruby to start building your Rails app
If you are new to web development and have chosen Rails to build your awesome web app, chances are you aren't familiar with Ruby. That's okay.
Most tutorials will tell you that you need to have a basic understanding of Ruby before you can get started. Nope!
Even though Rails is just a framework written in Ruby, it far overshadows it in terms of the mind space it occupies. At this point, the only reason to learn Ruby is so you can learn to build your exciting application with Rails. So, why wait?
If you have some familiarity with programming in any other popular language like Python or Javascript, this guide will allow you to skip past the awkward Ruby learning phase in <10 minutes. It will tell you all the strange Ruby things you will encounter when learning Rails and no more.
Ruby for Rails
“Ruby is a big language, but fortunately the subset needed to be productive as a Rails developer is relatively small.”
That’s from the popular Learn Enough Ruby on Rails book. It has a chapter called “Rails flavoured Ruby”. I’ll recommend reading that chapter after you’re done with this article.
I started learning Ruby on Rails by following the Getting Started guide on the Rails website. At this point, I did not know anything about the Ruby programming language. In order to complete the guide, I skipped past the terms and syntax that I didn’t understand. Bias for action, not knowledge.
I’ll be honest - it was frustrating at times. But I’ll still recommend this approach because it gave me a purpose to learn more about Ruby. Instead of just learning what other people said was “prerequisite Ruby knowledge”, I could focus on specific things that confused me.
Here are the top ones:
"helpers", "controller actions", "methods" are all different names for the same basic concept of "functions".
Methods are functions defined inside classes, used with their objects.
Controller actions are methods defined inside controller classes.
Helpers are functions that are available to controller classes.
The
@article
thing that we use to share data across controllers and views is actually something called "instance variable".
class ArticlesController < ApplicationController
def index
@articles = Article.all
end
end
Instance variables store the data for a class object. Read these 2 posts to understand instance variables:
Similarities with other languages
If you have dabbled in programming in other languages before, you’ll find it helpful to compare Ruby with those languages and understand the similarities and differences.
Ruby documentation has a cool page where they compare writing in Ruby with writing in some popular languages like C, C++, Python, Java, PHP and Perl.
I have experience with Python. So, I found the To Ruby From Python article particularly helpful. Here are my notes on the similarities and differences.
Similarities with Python
These similarities made me feel comfortable:
Using single-quotes or double-quotes for strings
Assigning variables
Booleans like "&&", "==", "||", "!"
There are lots of inbuilt methods to manipulate strings (
to_s
,split
,length
,empty?
, etc) and arrays (empty?
,include?
,sort
,reverse
,shuffle
,push
, etc)Ruby's "range" works similar to python's "slice"
Ruby has “hash” just like Python has “dictionary”
Classes have a
new
method that is used to create new instances (or objects)Classes can inherit from other classes using
<
operator
Differences with Python
These differences feel weird and at times, plain stupid:
Parantheses ( ) are optional when calling a method.
There’s an
unless
keyword that is equivalent to an "else not" combination.Functions have an implicit return - they return the last statement evaluated. Meaning that the
return
keyword becomes optional in many cases.There's something called "symbols" in Ruby which look like strings but don't work like them at all. They are used for hash keys. In the example below,
:name
is a symbol.
>> h1 = { :name => "Michael Hartl", :email => "michael@example.com" }
Rails makes symbols even weirder by introducing flexibility in their syntax
>> h2 = { name: "Michael Hartl", email: "michael@example.com" }
>> h1 == h2
=> true
When inside a class method, use of
self
keyword is optional.