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.

Bundler Gotcha

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

Five Rails Gotchas

It’s undeniable that Rails is a great framework to speedily build up your application. However, despite of its handiness, like other frameworks, Rails has its own flaws and is never a silver bullet. This post is going to show you some of the gotchas (or pitfalls you name it) I encountered while working with Rails.