June 24, 2019

What I found surprising in Ruby, as a PHP developer

After years of mainly writing PHP, I started working with Ruby and Rails. I always loved the ruby philosophy and especially DHH but I never got to really get into it. I’m also very aware of what Rails brought to frameworks of any languages. Many of the feature I love in PHP are heavily inspired by Rails.

That being said, I found many things fairly surprising. I’m discussing here what I think is worth mentioning, mixing both Rails and Ruby.

Everything described here is my sentiments, it can be wrong because I’m lacking knowledge in Ruby while I’m very confident in my PHP skills and community awareness. I’m using RVM and Bundler v2.

Learning a new language is not about the syntax but about understanding a new paradigm, acknowledging how people do things and apprehending the state of the community: tooling, dependencies, deployments and “best practices”.

Dependencies are installed globally

With Ruby, you’ll most likely install your dependencies with Bundler via the command bundle install. Coming from PHP, I expected a vendor folder to be created in my projects holding all my dependencies. It’s not what happened.

Instead bundler installed all the dependencies in folder shared by all projects. Each Ruby version have their own gems folder though.

Let’s say you already have a middleman project using version 1.2.3 of a gem. If you create a new Rails project that needs 1.5.0 of the same gem, it will install it and your middleman project will now use 1.5.0 too.

So every time, you switch projects, you run bundle install to install the correct version of the deps. There is a way to install gem in a local folder with the —path option but it doesn’t seem very popular.

Rails is less opinionated than you think

Coming from Laravel, I’m used to fully featured framework that comes with ORM, user management, model serialisation and such.

After I installed a fresh new version of Rails 6, I looked for the User model and the authentication flow. It didn’t exists, by default, Rails has no user management. Then, you start looking for a gem, but since you’re new to Rails: what should you choose?

The great news, is that Rails has something called [Application Templates](https://guides.rubyonrails.org/rails_application_templates.html to help you preconfigure a Rails with the gems you like, some default routes or extra commands.

By default, Rails comes with the famous ActiveRecord gem. I discovered that there are ways to replace it for Mongoid or Sequel for instance.

The issue I have with this, is that if you’re building a package (a gem) you can’t assume what the user have, and you kinda have (or should) maintain compatibility with alternatives to ActiveRecords. You can’t assume anything about the User model the app is using, you don’t have a easy way to serialize models, and so on.

I love how Laravel come with more by default.

You need a server to Rails (like Puma or Unicorn)

One thing I really liked about PHP is that it’s stateless. For every request, the whole thing is booted, nothing (unless you do it) is shared between request. It might not be the best for performances but that makes it very easy to reason about.

To run Rails, you need Puma or Unicorn running. I won’t get into details about it but I think it’s one more steps that makes Rails a bit hard to deploy.

I’m aware that Heroku makes easy but I like to host my project myself, on my bare metal server. At least at the beginning.

With that said, I'm loving Ruby right now. The language, the community and the mindset is everything I like. I'm glad I had the opportunity to work with Rails at Algolia.