Some basic concepts in Ruby and Ruby On Rails

Tram Ho

In this article, I will introduce the basic concepts of programming, in the Ruby language and in Ruby on Rails that not everyone knows.

1. ORM

ORM stands for Object Relational Mapping, is a programming skill that helps convert data between incompatible systems in object-oriented programming. It creates a virtual Object Database, which can be used inside a programming language.

In other words, an ORM framework is a layer in between the programming language and the database, written in an object-oriented programming language (such as Ruby, Python, PHP …) that you can use yourself. That programming language to manipulate the database without writing lengthy SQL statements. The object class will correspond to the data tables in the database, and the object instances will correspond to the records in those data tables.

In Ruby on Rails, Active Record is the module that plays the role of an ORM, which is responsible for handling database operations. Active Record retrieves data stored in database tables using rows and columns. Accessing or modifying these data, which is done by SQL statements (if you use SQL databases), has now been simplified into manipulating common Ruby objects.

2. STI

STI stands for Single Table Inheritance, the idea of ​​using a single table to reflect multiple models inherited from one parent model. It is a component of ActiveRecord::Base . In the database, the child model is identified by the type field. In Rails you just need to add a type field to the table and the system will understand that you are setting up an STI.

For example, you have the Admin model. The admin can be divided into two categories: Master or Manager. They have the same attributes but their behavior is different. Creating two Master and Manager tables is not necessary. Instead you only need to use an Admin table to store the data of both Master and Manager and they are distinguished by type field.

STI is used when the models have the same fields and functions. Instead of writing a function multiple times for different models or having the flexibility to add specific functions for each model, STI allows you to store the data of those models in a single table while It is still possible to write functions specific to each model. STI provides a full range of model methods in Rails such as create, new, update_attributes … for both parent classes and subclasses stored on a single table.

3. GC

GC stands for Garbage Collection, a module that provides an interface to Ruby's garbage collection mechanism. The most important and most commonly used method is GC.stat

GC.stat shows the parameters of GC, if running GC.stat in irb, we will get the result:

There is a fairly large amount of information here, but in reality there are only 2 things we should focus on :total_allocated_objects and :total_freed_objects . It shows all objects that are initialized and displays the total number of objects that are not in use.

You can find out more in detail in this article:

4. Benchmark

Benchmark is a necessary module when we want to verify the performance of a certain function or method. It can measure the runtime of any code. We only need the library before using:

To measure the time of a simple code we use the measure method

The following results:


  • user CPU time is the amount of CPU time spent on running its own tasks (running outside of the operating system kernel).
  • system CPU time is the amount of CPU time used to exploit resources, running in the kernel of the operating system.
  • Actual runtime is the most important parameter, telling us the exact time to execute the code.

However, the measure method is not used much. The reason is because it only prints the execution time of a piece of code. Benchmark is mainly used to compare the execution time of the code to solve the same problem. And the benchmark that has some methods of doing this is Benchmark#bm .

This method allows us to measure the runtime of some code and print the results to the screen in order. For example:

The following results:

5. Design Pattern

Design patterns are solutions that have been optimized and reused for the programming problems we encounter every day. It is a template that has been implemented and verified.

There are many different design patterns:

  • Template Method
  • Strategy
  • Observer
  • Composite
  • Iterator
  • Commands
  • Adapter
  • Proxy
  • Decorator
  • Singleton
  • Factory
  • Abstract Factory
  • Builder
  • Interpreter
Share the news now

Source : Viblo