Developing Web Applications in Ruby on Rails Can be a Time-Saver

Ruby on Rails = Gamechanger

Ruby on Rails can save you a lot of time when writing code. Write less, and still achieve the same goals as you would with other coding languages.

Krzysztof Kopydłowski
EL Passion Blog
Published in
6 min readOct 25, 2018

--

From Java to Ruby

Coding in Ruby has proven to be a real time-saver for me! I can put more effort into implementing methods and logic, rather than writing line after line of code to achieve the same end goal. I used to think that Java was the real game changer, but now that I’ve started working with Ruby on Rails, I must say, I’m super impressed.

For about a year, I wrote in Java as a back-end developer and I think that anyone who has written in this language will totally understand my experiences.

Let’s say that you want to write an endpoint that would return a zip file. The method that would archive the data to a zip file could easily have hundreds of lines of code…but we know we’re going to have to write much more code than just that.

But what if I told you that you could achieve the same result with just tens of lines of code. 😲

Most Developers New to Ruby Are Amazed by How Much Time They Save Writing Code

On Ruby

Ruby is an object-oriented, interpreted, and dynamic language. There are many web applications written in this language, like Github or Twitter.

So let’s talk about a few cool things in Ruby!

Ruby Requires Less Code

You might be asking yourself how it would be possible to implement the same logic as above, with so little code…I’m not even talking about using frameworks now, just plain language!

Ruby has many different methods that allow you to write far less code in a more readable way (at least in my opinion).

For example, if you have an array and you want to increment all elements by 2 and then return them in reverse order in Java, the code would look similar to this (with the use of List interface):

Fig.1 — Initialising, filling, incrementing and reversing array in Java

And in Ruby, the code would look like this:

Fig.2 — Initialising, filling, incrementing and reversing array in Ruby

As you can see it’s far less code 🎉 and you don’t have to import any classes at the beginning of your file!

Dynamic typing

After seeing the difference between how much code you need to write in Java and Ruby, you might be thinking why I didn’t specify the type of variable in the Ruby example. Another great thing about Ruby is that all variables are dynamically typed!

If you’re writing a method that takes two params, you don’t have to specify their types and instead just focus on the logic. As a result, you wouldn’t have to overload methods (actually, you couldn’t if you tried 🙈). This makes methods more divergent and again, less code = clearer code.

There are also many native methods in Ruby, that allow you to speed up your coding. Something I use quite often in my apps is converting Hash to Array, and then to JSON. In Ruby, I can make this conversion in just one line!

On Rails

The most popular framework for Ruby is Rails. It provides similar functionalities as Spring for Java. I want to share a few things I found really awesome about it! 🙂

All Rest Points in One Place

One of the things that I love about Rails is that I have a file in which I declare all of my endpoints. I can always type $ rake routes in terminal to see them. It’s especially useful in bigger applications when you have to write something and you want to see all rest services.

Another useful thing, about all rest points being in one place, is that you can divide your endpoints into groups! When you have a User model you can set up paths for its members so that each endpoint will take their id as a param automatically.

You don’t have set up the same endpoints with different params or explicitly specify them. In Rails, by default, you can pass any params to the endpoint and just validate those that you want to use in the controller.

Fig.3 — Permitting params we want to use

Setting up database with Rails

All migrations are written within the app so setting up your database on different devices is just the matter of ONE command: $ bundle rake db:setup. This means you don’t need an external client to set up or use a database.

You might think that even though you created and migrated your database on a different device it will be empty. But Rails provides us with another solution for that.

In your Rails app, there is a file called seeds.rb in which you can specify all of the entries for different models to get your app going. It’s just a few lines of code per model.

The command $ bundle rake db:setup does three things:

  1. It creates the database if it doesn’t exist
  2. It runs all migrations
  3. It seeds it up with the data from your seed file

Clear code — Just Logic in Entities Classes

Clear code in ActiveRecord class just writes methods you want to implement and not the attributes (you have a schema for that).

In Java’s framework Hibernate you would have to write all of the attributes with annotations at the top, then you would have to write setters for attributes that you want to modify. And at this point, there are already a lot of lines of code.

In Rails you don’t have to write a single line! Your whole db schema is stored in a file schema.rb which is automatically generated when you run migrations. In a class that represents your entity you don’t have to write any setters or attributes. When you want to get an attribute you just have to write: Model.attribute and that’s it!

You can focus more on coding and implementing methods for this class from the get-go.

Fig.4 — Getting price for the first trip and converting it to float in byebug mode

Super Easy Debugging

Fig. 4 shows one of the coolest things about Rails, which is debugging (or byebugging 😉). Byebug is a very popular gem in Rails and in order to use it you just have to add one line to your gemfile. After that, at any given moment in your code, you can write byebugand the execution of the code will stop at this line when it’s called (similar to breakpoint).

A terminal with your server or tests will stop at this moment and you will be able to call all the methods for the variables from your code that are available in this scope. To call them you simply have to type the name of it and you can see what it returns.

The really awesome part is that you can call methods on the objects from your database or see its states.

Fig.5— Summing price for all trips in database in byebug mode

It’s pretty clear that this speeds up the process of debugging! Instead of logging out your variables you can simply use them, as you would normally. I also really like the fact that byebug can be used in tests.

Conclusion

With Ruby on Rails, you can focus more on how to implement methods and logic and less on writing repeated code. It gives you powerful tools like dynamic typing or byebugging, that can speed up the developing process.

If you want to focus on developing web applications, then you definitely should write a small application in Rails to see how it works. Even if you still prefer your old technology, it never hurts to expanded your knowledge! 🙂

Try Ruby on Rails for Yourself

Tap the 👏 button if you found this article useful!

About the Author

Krzysztof Kopydłowski is a Backend Developer at EL Passion. You can find him on Linked In!

Find EL Passion on Facebook, Twitter and Instagram.

--

--