DEV Community

reyes2981
reyes2981

Posted on

what i learned last week: ruby on rails...a brief intro...

"So, do you know anything about frameworks?" my neighbor asked quizzically. The truth was I did not know what a framework was. I tried my best to logically think it through but I simply didn't know what that was and answered "no". I had that conversation back in July 2020 and now several months later I can say that I know what a framework is AND I know my way around Ruby on Rails, a framework. Over the course of this blog I'm going to give a brief introduction on Rails and show you the magic behind it!

What are Frameworks?

Back in the olden days when Farmville was a hit on Facebook I would sometimes add land to my virtual farm by purchasing expansion packs. Frameworks work in the same manner, they are bundles of code that you can add to existing code. A framework may include predefined classes and functions that can be used to process input, manage hardware devices, and interact with system software. This streamlines the development process since programmers don't need to reinvent the wheel each time they develop a new application.

Last year, I came across a metaphorical fork where I had to make a decision on what programming language I wanted to learn. As fate would have it, that language was Ruby.

What is Ruby?

Ruby is a programming language that was developed in 1995 by Yukihiro "Matz" Matsumoto. I'm grateful that this was the first programming language I learned in coding bootcamp. One of the biggest reasons is it's readability. Below is a method from one of the labs I am working on. If I showed my parents that code I know that while they won't be able to fully understand what is going on they would get the gist. As a newbie developer this has made learning new programming concepts and following programming logic that much easier.

def show
@category = Category.find(params[:id])
end

Another big concept to note about Ruby is that is is an object-oriented programming language (OOP). This means that it is based on the concept of "objects", which can contain data and code: data in the form of fields (often known as attributes or properties), and code, in the form of procedures (often known as methods).

So what does that mean? Lets reference back to the code above

def show
@category = Category.find(params[:id])
end

In the context of the above code we can deduce that this instance method's responsibility has something to do with locating a specific instance of an object using the given parameters. How is this all possible? Well with the power of object oriented programming, of course! Isn't it amazing how complex those three lines of code are once you really start digging into different parts of them?

Ok so Ruby is cool...can we talk about Rails yet?

No, I have one more reminder. Everything in Ruby is an object!

What is Ruby on Rails?

Ruby on Rails Directory

Ruby on Rails is a framework that was released in 2004. Some popular websites that use Rails include Airbnb, Crunchbase, Bloomberg, and Dribbble.

Four main things to know about Ruby on Rails

  1. MVC
  2. Convention over configuration
  3. Don't repeat yourself (DRY)
  4. Active record pattern

Model View Controller (MVC)

One important concept you are going to learn real quick in coding bootcamp is separation of concerns. For me, it wasn't until my Sinatra project that I really started to see the power of separation of concerns. This is where MVC makes it's grand entrance.

MVC is a software development pattern that divides the related program logic into three interconnected elements. The three components are the Model, View and Controller.

Alt Text

Model

  • The central component of the pattern
  • Responsible for storing and retrieving data

View

  • Responsible for displaying objects within an application
  • User Interaction

Controller

  • Responsible for accepting user input and responding to user input
  • Interacts with both the model and view

Convention over Configuration

Is a software design paradigm used by frameworks that attempts to decrease the number of decisions that a developer using the framework is required to make without necessarily losing flexibility.

For example...

Let's say that you have a table called posts with a primary id, the matching model, by convention, would be called post. The controller that handles all of program logic is named posts_controller. Lastly, the view is split into whatever actions are needed. For example, if these is a new action in the controller there is also a new view file.

Don't repeat yourself (DRY)

@posts.each do |post|
post.title
end

I remember when I first learned that something like the above code could be refactored into the below code my mind was blown. This is the basic concept of DRY.

@posts.each { |post| post.title }

DRY is a software development principle that aims at reducing repetition of code. Two other advantages of this principle are it's maintainability and readability. To learn more about Dry check out this website.

Active record pattern

The active record pattern is the M of the MVC pattern. It's also known as an ORM (Object-Relational-Mapping).

ORM is a technique for converting data between incompatible type systems using object-oriented programming languages.

Example:

Dog.find(50)

Above, thanks to the ActiveRecord gem, I'm able to use Ruby to interact with my Dog table which is in my SQL database.

Putting it all Together

Here is something to think about as I conclude this blog, how many forms a day do you fill online? Think about just the amount of things you google per day? When you start doing the math it adds up quickly. That is why Ruby on Rails, in my opinion, has been such a great first framework to worth with. It helps build programs that are used by almost everyone.

Conclusion

In a couple of days I will start creating my first Rails program. It feels like it was just yesterday I was learning about CSS and now I'm gearing up for my first project using a framework. This is my first blog in a weekly series that I will be writing to keep myself accountable. Thank you so much for reading. I'm open comments/feedback.

Top comments (0)