Skip to content

Instantly share code, notes, and snippets.

@pushcx
Last active June 14, 2017 18:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pushcx/3c30be556b393b5fb58a4845459e3e77 to your computer and use it in GitHub Desktop.
Save pushcx/3c30be556b393b5fb58a4845459e3e77 to your computer and use it in GitHub Desktop.
require 'binding_of_caller'
# https://twitter.com/garybernhardt/status/875037876215357440
class ReallyParticularInsantiation
def initialize
b = binding.of_caller(1)
# could also b.eval to get at __FILE__ and __LINENO__
if b.eval('self.class') != Foo or b.eval('__method__') != :try
raise RuntimeError, "I don't even know what to call this coupling"
end
puts 'ReallyParticularInsantiation#initalize allowed'
end
end
class Foo
def try
ReallyParticularInsantiation.new
end
end
class Bar
def try
ReallyParticularInsantiation.new
end
end
def kernel_method_try
ReallyParticularInsantiation.new
end
Foo.new.try
Bar.new.try
kernel_method_try
ReallyParticularInsantiation.new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment