Skip to content

Instantly share code, notes, and snippets.

@gingray
Created July 2, 2025 08:55
Show Gist options
  • Save gingray/7bbc0980807abf78bed621959999b3cf to your computer and use it in GitHub Desktop.
Save gingray/7bbc0980807abf78bed621959999b3cf to your computer and use it in GitHub Desktop.
Patch ruby class for debugging purposes
# 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