diff --git a/README.md b/README.md index 97166c9..0f95ee4 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/ruby/check-if-an-object-includes-a-module.md b/ruby/check-if-an-object-includes-a-module.md new file mode 100644 index 0000000..0017feb --- /dev/null +++ b/ruby/check-if-an-object-includes-a-module.md @@ -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)