Skip to content

Instantly share code, notes, and snippets.

@eregon
Created May 15, 2020 20:13
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 eregon/61ff7e20aaaa5af35a3e6401603d1f7f to your computer and use it in GitHub Desktop.
Save eregon/61ff7e20aaaa5af35a3e6401603d1f7f to your computer and use it in GitHub Desktop.
def enclosing_module
begin
mod = self.name.split("::")[0..-2].inject(Object) { |obj, c| obj.const_get(c) }
if mod < FFI::Library || mod < FFI::Struct
mod
elsif mod.respond_to?(:find_type)
arity = mod.method(:find_type).arity
# Only consider a #find_type that accepts a single argument.
# MakeMakefile#find_type from 'mkmf' needs 2 arguments and should be ignored.
if arity == 1 || arity == -1 || arity == -2
mod
end
end
rescue Exception
nil
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment