1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 07:08:01 +00:00

Add Check If An Object Includes A Module as a Ruby til

This commit is contained in:
jbranchaud
2021-12-15 16:05:06 -06:00
parent 6d542ea5f1
commit 4045d97d63
2 changed files with 27 additions and 1 deletions

View File

@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
_1170 TILs and counting..._
_1171 TILs and counting..._
---
@@ -896,6 +896,7 @@ _1170 TILs and counting..._
- [Block Comments](ruby/block-comments.md)
- [Build HTTP And HTTPS URLs](ruby/build-http-and-https-urls.md)
- [Chaining Multiple RSpec Change Matchers](ruby/chaining-multiple-rspec-change-matchers.md)
- [Check If An Object Includes A Module](ruby/check-if-an-object-includes-a-module.md)
- [Check Return Status Of Running A Shell Command](ruby/check-return-status-of-running-a-shell-command.md)
- [Click On Text With Capybara](ruby/click-on-text-with-capybara.md)
- [Colorful Output With MiniTest](ruby/colorful-output-with-minitest.md)

View 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)