Skip to content

Instantly share code, notes, and snippets.

@caius
Created May 10, 2014 06:56
Show Gist options
  • Save caius/75467c51b545a6c0f56a to your computer and use it in GitHub Desktop.
Save caius/75467c51b545a6c0f56a to your computer and use it in GitHub Desktop.
class RandomUnavailable < StandardError
attr_reader :unavailable_method
def self.for(*methods)
Module.new do
singleton_class.send(:define_method, :included) do |klass|
klass.instance_eval do
methods.each do |m|
define_method(m) { raise RandomUnavailable.new(m) }
end
end
end
end
end
def initialize(unavailable_method)
@unavailable_method = unavailable_method
end
def message
"#{unavailable_method} is not available for use"
end
end
module Kernel
include RandomUnavailable.for(:rand, :srand)
singleton_class.send :include, RandomUnavailable.for(:rand, :srand)
end
class Random
include RandomUnavailable.for(:rand, :srand)
singleton_class.send :include, RandomUnavailable.for(:rand, :srand)
end
class Array
include RandomUnavailable.for(:shuffle, :sample)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment