Ruby Journal

Quick Guide to ActiveSupport::HashWithIndifferentAccess

| Comments

Hash is beautiful. It is one of many things why I love Ruby. As you might have known, Hash is identified by a key, this key could be either a String or a Symbol. For most of cases, people tend to go for symbol because it would take up less memory (though it might come with a side effect that is Symbol is not GC-colletable (Ruby 2.2.0 does clean it up though)).

There are access usecases that requires our hash key to be interchangable between String and Symbol key. For example, web application request parameter processing.

We could typecast the key to either String or Symbol but it would soon emerge an annoying pattern. Instead, with the help of ActiveSupport, you can create a hash with no differences if accessing using String or Symbol key. Introducing ActiveSupport::HashWithIndifferentAccess.

How to Integrate Sidekiq With ActiveJob

| Comments

One of the hot thing in Rails 4.2 is the brand new ActiveJob gem, this gem consolidate the API for background job gems on the market such as DelayedJob, Resque, etc. Today I am going to guide you through how to integrate Sidekiq with ActiveJob, and you will learn:

  • Set up Sidekiq adapter for ActiveJob
  • Basic of ActiveJob class
  • Advanced usage of multiple queues
  • ActiveJob callback
  • ActiveJob exception catch
  • ActiveJob mailer API

How to Solve Silicon Straits Saigon Hiring Challenge

| Comments

Hello folks

For whom who might have not heard of Silicon Straits Saigon (SSS), this company is one cool company in Vietnam with strong focus in tech such as iOS and Web. When I use the word cool, I did not mean it before I touch base with their hiring challenge.

SSS is known to have a quite unique way to challenge potential employees. They give you a very cryptic page http://hiring.siliconstraits.vn and asks you to hack them.

Well, today I am going to show you how to solve this problem step by step. I hope Mr. An (Director of SSS) won’t hate me for this.

Spoiler alert! Go give the challange a go yourself before reading!

How to Write Custom Serializer for ActiveRecord#serialize

| Comments

Rails comes with a powerful and convenient serialize method that would do the serialization/deserializtion for a specify column of an ActiveRecord model. In today tutorial, I’ll walk you through on how to write a custom serializer that would encrypt/decrypt your serialized value for extra security.