ruby

Getting Familiar with RSpec in Rails

I spent some time over the weekend getting familiar with RSpec. Gonna brain dump (with just a little bit of structure) the process and what I did and learned. To start I set up in a new rails project and kinda tweaked it into a place where I can be productive. What is RSpec though? It is a testing framwork. But it's a little different than the Minitest testing framework that ships with Rails.

Continue reading →

The difference between length, size, and count in Ruby on Rails

I was asked recently if I could explain the difference between length, size, and count for ActiveRecord models in Ruby on Rails. Unfortunately I had no answer. But I wanted to really understand so I dug into the API docs. On a Ruby Array the methods size, count, and length are as such; size is an alias for length, length simply returns a count of the all elements in the array, and count is pretty neat in that you can pass in a block to count elements based on some condition.

Continue reading →

Enforcing Interfaces in Ruby

Ruby doesn't have the concept of an interface. Unlike say, PHP. In PHP you can specify that a class has to act like or implement specific methods. If your class fails to honor the contract of that interface and does not implement the methods of the interface, you get an error during runtime. Ruby does not have this. But we can ensure our code honors the contract of an interface by writing unit tests for our classes.

Continue reading →

Deploying a Simple Rails App with Ansible

Ruby on Rails is quickly becoming my framework of choice for my personal websites and projects. It's a pleasure to work with and has been easy to learn. But no framework is without its challenges. One of those challenges is of course deploying the app to a server. There are a lot of options for hosting and deploying a Rails app. But, I like to run my own servers which means I have to also take care of deploying to those servers.

Continue reading →

How to Run Rails App Server with Systemd and Ansible

Create a systemd service to run your rails app server. Ansible tasks to create the service: --- …snip… vars: rails_root: “/myapp” rails_user: “webuser” tasks: - name: Setup Rails Web Service template: dest: /usr/lib/systemd/system/rails-web.service src: templates/rails-web.systemd.j2 - name: Enable Rails Web Service systemd: name: rails-web daemon_reload: yes enabled: yes masked: no The ansible template "rails-web.systemd.j2": [Unit] Description=Rails Web [Service] Type=simple SyslogIdentifier=rails-web User={{ rails_user }} PIDFile={{ rails_root }}/tmp/pids/web.pid WorkingDirectory={{ rails_root }} ExecStart=/bin/bash -l -c “{{ rails_root }}/bin/rails s -b 0.

Continue reading →