Skip to content

Instantly share code, notes, and snippets.

@hanachin
Last active May 29, 2019 03:45
Show Gist options
  • Save hanachin/fd6d42c431fd1b700bd49863a9773fe3 to your computer and use it in GitHub Desktop.
Save hanachin/fd6d42c431fd1b700bd49863a9773fe3 to your computer and use it in GitHub Desktop.
module Hi
instance_methods_snapshot = []
TracePoint.trace(:class) do |tp|
next unless tp.self == self
# Hi.singleton_class.prepend Module.new { define_method(:method_added) { "ここで最小限のスナップショットを取ることも可能" } }
instance_methods_snapshot = instance_methods.map(&method(:instance_method))
end
TracePoint.trace(:end) do |tp|
next unless tp.self == self
instance_methods_snapshot.each do |instance_method_snapshot|
method_name = instance_method_snapshot.name
current_instance_method = instance_method(method_name)
next if instance_method_snapshot == current_instance_method
module_eval do
define_method(method_name) do
instance_method_snapshot.bind(self).call
current_instance_method.bind(self).call
end
end
end
end
end
@hanachin
Copy link
Author

ツイート可能

module Hi
  m=nil
  TracePoint.trace(:class) { |tp| tp.self == self && m=instance_method(:hi) rescue nil }
  TracePoint.trace(:end) { |tp| next unless tp.self == self; (m2=instance_method(:hi) rescue nil)&.==m&.== define_method(:hi) { m.bind(self).call; m2.bind(self).call } }
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment