Last active
December 29, 2016 11:38
-
-
Save theHamdiz/23aec1873fd5197e19cb to your computer and use it in GitHub Desktop.
Class methods are not included they are extended, see this gist for further explanation on the major difference between including a module and extending it.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module A | |
| def our_method | |
| puts "Module A" | |
| end | |
| end | |
| class Including | |
| # by including a module we mark its methods as instance methods | |
| # that can only be used from the object scope | |
| include A | |
| end | |
| class Extending | |
| # extending a module marks its methods for being Class-Level methods | |
| # you can't call them from the object scope | |
| # only from the class level | |
| extend A | |
| end | |
| msg = <<EOF | |
| Hope this illustrated the difference between including | |
| modules and extending them in ruby, also illustrated | |
| Ruby's error processing mechanism in details | |
| EOF | |
| [Including, Extending].each do |klass| | |
| begin | |
| puts "Working on the #{klass.to_s} class" | |
| klass.our_method | |
| rescue NoMethodError => err | |
| puts err, "We're gonna fix it..." | |
| klass.new.our_method | |
| ensure | |
| puts '-' * 18 | |
| end | |
| end | |
| puts msg |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment