Created
March 4, 2012 01:16
-
-
Save gabebw/1969764 to your computer and use it in GitHub Desktop.
module_function is not the same as self.method
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
| # This works | |
| module TestOne | |
| def module_method | |
| puts "TestOne works" | |
| end | |
| module_function :module_method | |
| class A | |
| include TestOne | |
| def instance_method | |
| module_method | |
| end | |
| end | |
| end | |
| # This fails | |
| module TestTwo | |
| def self.module_method | |
| puts "TestTwo fails" | |
| end | |
| class A | |
| include TestTwo | |
| def instance_method | |
| module_method | |
| end | |
| end | |
| end | |
| TestOne::A.new.instance_method | |
| TestTwo::A.new.instance_method | |
| # Output: | |
| # | |
| # TestOne works | |
| # module_function.rb:25:in `instance_method': undefined local variable or method `module_method' for #<TestTwo::A:0x0000010084d400> (NameError) | |
| # from module_function.rb:31:in `<main>' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment