# Summoned into existence by my co-worker Ehren
def confusicate_class(klass)
methods = {}
(klass.instance_methods - Object.instance_methods).each do |meth|
methods[meth] = klass.instance_method(meth.to_sym)
end
klass.class_eval do
methods.keys.each do |meth_name|
define_method(meth_name) do
current = methods.keys[rand(methods.keys.size)]
meth = methods[current]
methods.delete(current)
meth.bind(self).call
end
end
end
end
class Foo
def one; 1; end
def two; 2; end
def three; 3; end
end
confusicate_class(Foo)
f = Foo.new
%w{one two three}.each do |n|
puts "#{n} => #{f.send(n)}"
end
# OUTPUT
# ~/$ ruby r.rb
# one => 3
# two => 1
# three => 2
# ~/$ ruby r.rb
# one => 3
# two => 2
# three => 1
# ~/$ ruby r.rb
# one => 2
# two => 1
# three => 3