mirror of
https://github.com/jbranchaud/til
synced 2026-01-04 23:58:01 +00:00
Add Check If An Object Includes A Module as a Ruby til
This commit is contained in:
25
ruby/check-if-an-object-includes-a-module.md
Normal file
25
ruby/check-if-an-object-includes-a-module.md
Normal file
@@ -0,0 +1,25 @@
|
||||
# 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](https://ruby-doc.org/core-3.0.0/Module.html#method-i-include-3F).
|
||||
|
||||
```ruby
|
||||
# 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](https://stackoverflow.com/a/28667632/535590)
|
||||
Reference in New Issue
Block a user