Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Object
- def behaves_like?(object)
- expected_methods =
- if object.respond_to?(:instance_methods)
- object.instance_methods
- else
- object.methods
- end
- (expected_methods - methods).empty?
- end
- end
- class Foo
- def method1; end
- end
- class Bar
- def method1; end
- def method2; end
- end
- foo = Foo.new
- bar = Bar.new
- p foo.behaves_like?(Bar) # => false
- p foo.behaves_like?(bar) # => false
- p bar.behaves_like?(Foo) # => true
- p bar.behaves_like?(foo) # => true
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement