Quần Cam

Monkey Patching for good

Monkey-patching has been widely considered as bad practice in software development, in terms of source code management and maintainability.

Monkey Patch For Good

Anyway, I believe everything has its own reason to exist, and below are something might change your mind about Monkey Patching.

Find yourself in development environment

Tired of keep typing something like User.find_by_username('jack') in Rails console? Use this.

Monkey patch your object class in .irbrc


require 'irb/completion'
require 'irb/ext/save-history'

class Object
  def find_me
    self.find_by_username '[email protected]'

Then now User.find_me will return your favourite test user.

Inspect Object

Tired of keep typing this?

users = User.all
p users.to_sql
users = users.order(username: :desc)
p users.to_sql

Then this might save your time.

class Object
  def show_me(method)
    tap { |obj| puts obj.send(method) }

# then now
User.all.show_me(:to_sql).order(username: :desc).show_me(:to_a)

Convinced yet? Leave your thoughts below!

Khuyến cáo giữ chặt bàn phím và lướt thật nhanh khi đi qua khu vực này.
Chức năng này hỗ trợ markdown và các thứ liên quan.

Bài viết cùng chủ đề

Euruko 2017 Notes

Vừa rồi mình đi Euruko 2017 ở Budapest, một số bài nói cũng khá thú vị nên mình sẽ note lại ở đây.

Six confusing features in Ruby

In this post I am trying to point out some Ruby features you might want to use with a lot of caution.

Bundler Gotcha

A few days ago I encountered a strange behavior of Bundler so this post notes down how my experience with it was.