Ruby refinements

From wikinotes
Revision as of 01:53, 19 November 2021 by Will (talk | contribs) (Created page with "Refinements let you limit the scope of Monkey-Patches.<br> You can override methods, or add new ones, and they will only be applied to classes/modules if they are explicitly i...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Refinements let you limit the scope of Monkey-Patches.
You can override methods, or add new ones, and they will only be applied to classes/modules if they are explicitly included.

Documentation

official docs https://docs.ruby-lang.org/en/3.0.0/doc/syntax/refinements_rdoc.html

Example

class User
end
module Passenger
  refine User
    def buckle_seatbelt; end
  end
end
# bind monkeypatch within this scope
using Passenger

# use monkeypatch
user = User.new
user.buckle_seatbelt