1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 15:18:01 +00:00
Files
til/ruby/check-if-an-object-includes-a-module.md

756 B

Check If An Object Includes A Module

You may want to know if an object's class includes a module because that will tell you something about the object's behavior. It is another way of asking if an object responds to a method or set of methods, assuming you know what methods the module provides.

This can be done with the Module#include? method.

# assuming some object book of type Book that includes Rateable
> book.class
=> Book
> book.class.include?(Rateable)
=> true

# assuming some object author of type Author that doesn't include Rateable
> author.class
=> Author
> author.class.include?(Rateable)
=> false

source