Skip to content

Instantly share code, notes, and snippets.

@acook
Last active August 29, 2015 13:55
Show Gist options
  • Save acook/8694054 to your computer and use it in GitHub Desktop.
Save acook/8694054 to your computer and use it in GitHub Desktop.
class Module
# is there a given distinct method defined on the instance of a class?
def distinct_method_defined? method_name
distinct_instance_methods.include? method_name.to_sym
end
# display the methods - including inherited - that distinguish this class's instances
def distinct_instance_methods
exclude = self == Object ? Kernel.public_instance_methods : Object.public_instance_methods
public_instance_methods - exclude
end
end
Array.method_defined? :tap
#=> true
Array.method_defined? :flatten
#=> true
Array.distinct_method_defined? :tap
#=> false
Array.distinct_method_defined? :flatten
#=> true
class SubArray < Array
def foo
p __method__
end
end
SubArray.method_defined? :tap
#=> true
SubArray.method_defined? :flatten
#=> true
SubArray.method_defined? :foo
#=> true
SubArray.instance_methods(false).include? :tap
#=> false
SubArray.instance_methods(false).include? :flatten
#=> false
SubArray.instance_methods(false).include? :foo
#=> true
SubArray.distinct_method_defined? :tap
#=> false
SubArray.distinct_method_defined? :flatten
#=> true
SubArray.distinct_method_defined? :foo
#=> true
Object.instance_methods.length
#=> 138
Object.instance_methods(false).length
#=> 32
Object.distinct_instance_methods.length
#=> 64
Kernel.instance_methods.length
#=> 74
Kernel.instance_methods(false).length
#=> 74
Kernel.distinct_instance_methods.length
#=> 0 # has no distinguishing methods
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment