What I learned while building 3 projects with Ruby on Rails.

At the time of this writing I am a student at Microverse. Part of the technical curriculum is Ruby and Ruby on Rails.

Before I started the curriculum I was already proficient in Javascript and love it.

So what is Ruby and what is special about it?

Ruby is a higher-level language just like Python and Perl. Ruby got many good ideas from Perl, Lisp, Smalltalk, Eiffel, and Ada. It is dynamically typed so there is no need to declare your variable’s types upfront. It is interpreted and can be modified at runtime. This means you can add new methods to classes while it is running. It has dozens of shortcuts that make it very clean.

You can create packages that do a specific job and reuse them in other projects. These packages are called gems and to paraphrase the old iPhone add, for anything you need to do “there’s a gem for that”.

Ruby has some key differences from other older languages like Javascript or Java. These might look terrible at the start but you can not live without them later. These Ruby-isms are useful in better understanding the Rails method syntax.

  • Parentheses on method calls are optional. Yes that’s right. You can call a method using the arguments a, b like this
def my_add a, b
a + b
end
  • Blocks do not need curly braces. They start with “do” and end with an “end”
for i in (1..100) do
result << array.count {|e| e.to_i < i}
end
  • Semicolons aren’t needed after each line. That was crazy for me coming from a Javascript background.
  • Parentheses aren’t needed around the conditions in if-then statements. You can put the if after your statement
x = b if a > b
  • Use “if-then-else-end” without curly brackets.
if a > b
x = 1
else
x = 2
end
  • Methods automatically return the last line. You can call return explicitly if needed.

Ruby variables

  • x = 3 is a local variable for a method or block and it is gone when the method is done
  • @x = 3 is an instance variable owned by each object (it sticks around)
  • @@x = 3 is a class variable shared by all objects (it sticks around, too).
  • :hello is a symbol, like a constant string. Useful for indexing hashes. See below.
  • Object definitions are epic. object = { :key1 => "Name", :key2 => "Address" } is a hash of key/value pairs. For a language so lean as Ruby I would expect something better. Latest versions copied the Javascript way of writing objects so the above can also be written much cleaner object = { key1: "Address", key2: "Value" }

Ruby variable assignments

  • The most exciting one is the || operator. If you use it like this
x = a || b || c || 'default value' 

it evaluates each variable after the = sign and assigns the first true one. There is a shortcut also

x ||= b

This means “if x has a value keep it else assign b”.

Which brings us to Ruby on Rails, Rails for short.

What is Rails?

Rails is a web development framework. It helps make web applications, providing classes for all the database operations. It handles URLs and displays HTML. It comes with a webserver, maintenance tasks, a debugging console, and much more. And it is available as a free gem!

Ruby uses the Model View Controller (MVC for short) design pattern. MVC brings longevity, testability, and maintenance to your project. This is ideal for most web applications. So what this mean?

Model is the part of Ruby that deals with your data. The Model has all the classes needed for the CRUD operations.

The View displays all the data in HTML format usually. There are options for JSON and XML also. View also determines which events are passed to the controller.

The Controller handles all the events passed from View. It takes user input, like a URL, and decide what to do (show a page, order an item, post a comment). It may initially have business logic, like finding the right models or changing data. As I learned this is an anti-pattern. As your Rails knowledge increases you will move business logic into the model. (as this article suggests fat model, skinny controller).

When you create your first Rails app you will get a well-organized directory. Most of your work will be done inside the app folder which includes the model, view, and controller folders.

Class and table names are important.

Rails prefer convention over configuration. It has certain naming conventions. These are important because they let rails applications “find their parts” easily, without any configuration. It expects objects from the class Articles to be saved to a database table named Article. Yes, Rails has a pluralization engine to figure out what object maps to what table. This is great and scary at first when you’re not sure how classes and tables are getting linked together. It also makes it very easy for a programmer to understand any Rails app.

Conclusion.

So there you have it. These are the most interesting parts of Rails and Ruby. Of course there are a lot more. For all these Ruby makes fast development easy. Do not forget many great sites were created and most of them still use it.

If you liked this article please spare a few claps. Also please share with anyone you think might find it useful.

For any comments and suggestions, don’t hesitate to contact me directly.

Thank you for reading.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Nick Haralampopoulos

Nick Haralampopoulos

Works as Software Engineer at PropTechCore. Full Stack Developer, Microverse Alumni, Automotive Engineer, Entepreneur, Motorcycle racer, Aircraft lover.