Ruby Journal

How to Open/override/monkey-patch a Class in Ruby?

| Comments

So you search for how to moneykey-patch a class in Ruby? Read on, I’ll show you how.

Note

Ruby is very powerful and flexible. Before I show you how to override a class, I want to ensure that you understand that monkey-patching is not considered a good practice. It might be the source of hard to trace bugs.

Open existing class by re-defining class

Imagine we have following class:

1
2
3
4
5
class Greeter
  def hello
    "hello world"
  end
end

You can open your existing class easily by re-declaring the class and its methods:

1
2
3
4
5
class Greeter
  def hello
    "hello Uncle!"
  end
end

The Greeter#hello would be overriden with new message if class Greeter does exist.

If class Greeter has not been defined, then our above code would define a new Greeter class. And that could lead to side effect if we are not careful.

Open existing class with class_eval

Alternatively, Ruby provide class_eval method which would check for the existence of the to-be overriden class. If the class does exist, the block of code that is parsed to class_eval would be evaluated and replace the existing methods. So our example above will be:

1
2
3
4
5
Greeter.class_eval do
  def hello
    "hello WORLD!"
  end
end

If class does not exist, an NameError exeception will be raised:

1
2
3
4
5
6
NonExistingGreeter.class_eval do
  def hello
    "hello WORLD!"
  end
end
#=> NameError: uninitialized constant NonExistingGreeter

Conclusion

Be aware of your action before you override a class and I highly recommend you to use class_eval because it does gives you extra class existence check.

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 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.

Liquid Template in Ruby Done Right

| Comments

Liquid Templating Engine is an awesome technology and with the power of gem ‘liquid’, everybody can start using without much hassles. I saw many projects used this gem but sadly most of them are quite bad. In this tutorial, I’ll go through a bad example and show you how to refactor it.