How good Ruby on Rails is and how it speeds up development

    This post is a translation of the article by programmer Krzysztof Kopidlovski, devoted to the analysis of the benefits of Ruby on Rails. The material will be interesting first of all to novice programmers and only then to professionals.

    Ruby on Rails will help save the time you usually spend on development. Just because when using this framework code will be less, and the functionality will remain the same.

    Skillbox recommends: Practical course "Profession web developer . "

    We remind: for all readers of "Habr" - a discount of 10,000 rubles when writing to any Skillbox course on the promotional code "Habr".

    Java to Ruby

    For me, working with Ruby is a real time saver. I can concentrate on methods and logic, and not mess around with a lot of code line by line. I used to think that Java is a game-changer, but after meeting Ruby on Rails, I was even more impressed.

    About a year I write in Java and do backend development. It seems to me that any programmer working with this language will understand what I'm talking about. Suppose you want to write an endpoint to return a .zip file. You can solve this problem without any problems, but this requires hundreds of lines of code.

    But what if I tell you that you can get by with a few dozen lines?

    This is Ruby - an object-oriented, dynamic, reflective, interpretable high-level programming language. It has a multithreading implementation independent of the operating system, strong dynamic typing, garbage collector and many other features. A lot of web applications are written on it, including GitHub and Twitter.

    Ruby reduces code

    Probably, you are asking yourself how it is generally possible to reduce the amount of code to several dozen lines while preserving the logic mentioned above. It is really possible, and we are not talking about frameworks yet, it’s about pure language.

    For example, if you have an array and you want to increase all the elements by 2, and then return them in reverse order in Java, the code will look like this (using the List interface):

    import java.util.*;
    importstatic java.util.Collections.reverseOrder;
        publicstaticvoidmain(String args[]){
            List<Integer> list = Stream.of(1, 2, 3)
              .map(val -> val + 2)

    The same thing on Ruby would look like this:

    array = (5.2) {| x | x + 2} .reverse

    The difference is obvious. And you do not need to import classes.

    Dynamic typing

    After we made sure that there may not really be too many lines of code, we can think about the fact that in the example above the type of the variable is not set. This is true - the fact is that in Ruby all variables are dynamically typed.

    If you are writing a method that takes two parameters, you do not need to specify their types; instead, you can simply focus on the logic. Result: less code, with better and cleaner.

    Ruby also has many proprietary methods that speed coding. When writing programs, I most often use the Hash to Array transformation, and then JSON. In Ruby, I can do it in just one line!

    On rails

    The most popular framework for Ruby is Rails. It provides roughly the same language functionality as Spring for Java. I would like to share some of my findings that may be useful to you.

    All endpoints in one place.

    One of the things I like about Rails is that I only need one file to declare all my endpoints. I can always use the $ rake routes terminal command to see them. This is a great option for large projects when you need to do something based on what is already written.

    In addition, you can divide your endpoints into groups. For example, when you have a User model, you can set paths for all its members so that each endpoint automatically receives its own identifier.

    You do not have to use different parameters for the same endpoints. In Rails, by default, you can pass any parameters to the endpoint and just validate for those that you would like to use in the controller.

    def user_params
    params.require (: user) .permit (: name,: surname,: birth_date,: avatar)

    Rails databases

    All migrations are written here in the application, so setting up the database on different devices is reduced to executing one command: $ bundle rake db: setup. Thus, an external client for setting up or using the database is simply not needed.

    And no, the database that you created and transferred to another device will not be empty: in your Rails application there is a file called seeds.rb, in which you can specify all entries for different models required for the application to work. As a result, the model needs only a few lines of code.

    The $ bundle rake db: setup command performs three functions:

    • Creates a database if it does not already exist;
    • Runs all migrations;
    • Logs all raw data from your seed file.

    The code is really clean: ActiveRecords simply records the methods you need to implement, not the attributes.

    In the Hibernate framework for Java, you need to prescribe all attributes with annotations, and then also setters for the attributes that you need to modify. In this case, you get a large amount of code at the output.

    In Rails, the same thing takes one line. DB Schema is saved in the file schema.rb, which is automatically created when migration starts. And the class does not need setters or attributes. When the latter are required, it will suffice to write: Model.attribute - and that’s all.

    As mentioned above, with Rails you can focus specifically on the logic and methods of your project, and not on the code.


    Ruby on Rails gives you powerful tools like dynamic typing or byebugging that speed up the programming process pretty well.

    If you want to take a course on developing web applications, be sure to try writing a small Rails application to understand how it works. It’s never too late to learn something new - even if you prefer traditional tools.

    Also popular now: