Created
February 15, 2012 21:06
-
-
Save brainopia/1839012 to your computer and use it in GitHub Desktop.
This file contains 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
# power of extend | |
# can be used to add methods only to top-level object | |
module Rake | |
def task | |
:executed | |
end | |
end | |
extend Rake | |
task # => :executed | |
Object.new.task # => NoMethodError | |
# respects method visibility scopes contrary to module_function | |
module Friends | |
extend self | |
def joey | |
"how're you doing" | |
end | |
private | |
def rachel | |
"i'm nacked, get out" | |
end | |
end | |
Friends.joey # => how're you doing | |
# if we'd used module_function instead of extend self then next lines would return NoMethodError | |
Friends.rachel # private method `rachel' called for Friends:Module | |
Friends.send(:rachel) # => i'm nacked, get out |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment