Skip to content

Instantly share code, notes, and snippets.

@noahgibbs
Created September 22, 2023 09:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noahgibbs/7ce6f31357794fa992beff78368a10b9 to your computer and use it in GitHub Desktop.
Save noahgibbs/7ce6f31357794fa992beff78368a10b9 to your computer and use it in GitHub Desktop.
Stochastic method definition
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