Created
September 22, 2023 09:53
-
-
Save noahgibbs/7ce6f31357794fa992beff78368a10b9 to your computer and use it in GitHub Desktop.
Stochastic method definition
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
class BadIdea | |
def self.method_added(method_name) | |
return if @mid_define | |
@obj ||= Object.new | |
@old_methods ||= {} | |
@old_methods[method_name] ||= [] | |
@old_methods[method_name] << self.instance_method(method_name) | |
@mid_define = true | |
methods = @old_methods[method_name] | |
self.define_method(method_name) do |*args, **kwargs, &block| | |
m = methods.sample | |
m.bind_call(self, *args, **kwargs, &block) | |
end | |
@mid_define = false | |
end | |
def print; puts "a"; end | |
def print; puts "b"; end | |
end | |
a = BadIdea.new | |
10.times { a.print } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment