Created
July 2, 2025 08:55
-
-
Save gingray/7bbc0980807abf78bed621959999b3cf to your computer and use it in GitHub Desktop.
Patch ruby class for debugging purposes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
module RShade | |
module Patcher | |
class ObjectPatcher | |
def initialize | |
end | |
def patch(object, method, type = :inst, &action_block) | |
module_name = "RShadeDynModule#{object}#{method}" | |
m = Object.const_set(module_name, Module.new) | |
m.define_singleton_method(:prepended) do |base| | |
original_method = "#{method}_original" | |
if type == :inst | |
base.alias_method(original_method, method) | |
else | |
base.singleton_class.alias_method(original_method, method) | |
end | |
method_definition = type == :class ? 'define_singleton_method' : 'define_method' | |
base.send(method_definition, method) do |*args, **kwargs, &block| | |
action_block.call(*args, **kwargs) | |
result = send(original_method, *args, **kwargs, &block) | |
result | |
end | |
end | |
object.prepend(m) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment