Ruby cho phép mở lại bất kỳ class nào — kể cả built-in như String, Integer — để thêm hoặc ghi đè method.
Gọi là open class hay monkey patching.
class Integer
def factorial
return 1 if self <= 1
self * (self - 1).factorial
end
end
5.factorial # => 120Ưu điểm: DSL ngắn gọn, tự nhiên (Rails dùng rộng rãi: 2.days.ago). Nhược điểm: ghi đè ngầm method của gem khác → bug khó trace; upgrade Ruby/gem có thể conflict; khó biết method đến từ đâu.
Thay thế an toàn hơn: Refinement (refine ... using) — monkey patch chỉ có hiệu lực trong scope khai báo using.
Ruby allows reopening any class — including built-ins like String and Integer — to add or override methods.
This is known as open classes or monkey patching.
class Integer
def factorial
return 1 if self <= 1
self * (self - 1).factorial
end
end
5.factorial # => 120Pros: concise DSLs (Rails uses this heavily: 2.days.ago). Cons: silently overrides gem methods → hard-to-trace bugs; Ruby/gem upgrades may conflict; hard to tell where a method originates.
Safer alternative: Refinements (refine ... using) — monkey patches scoped to only where using is declared.