Refinement (Ruby 2.0+) là cách mở rộng class có phạm vi giới hạn — method chỉ có hiệu lực trong file/module đã khai báo using, không lan ra toàn global như monkey patch.
ruby
module StringExtras
refine String do
def palindrome?
self == reverse
end
end
end
# NGOÀI scope using — chưa có hiệu lực
"racecar".respond_to?(:palindrome?) # => false
# BÊN TRONG scope using
using StringExtras
"racecar".palindrome? # => true
"hello".palindrome? # => false| Monkey Patch | Refinement | |
|---|---|---|
| Phạm vi | Global — ảnh hưởng toàn app | Lexical scope — chỉ sau using |
| An toàn | Thấp — conflict với gem khác | Cao — cô lập |
| Hiệu năng | Bình thường | Nhẹ hơn do không thay đổi global vtable |
Dùng Refinement khi cần thêm method vào built-in class mà không muốn ảnh hưởng code ngoài scope của mình.
A Refinement (Ruby 2.0+) extends a class in a limited scope — methods are active only within files/modules that call using, not globally like a monkey patch.
ruby
module StringExtras
refine String do
def palindrome?
self == reverse
end
end
end
# OUTSIDE using scope — not visible
"racecar".respond_to?(:palindrome?) # => false
# INSIDE using scope
using StringExtras
"racecar".palindrome? # => true| Monkey Patch | Refinement | |
|---|---|---|
| Scope | Global — affects entire app | Lexical scope — only after using |
| Safety | Low — conflicts with gems | High — isolated |
Use Refinements when you need to extend a built-in class without polluting the global namespace.