Skip to content

Instantly share code, notes, and snippets.

@brainopia
Created February 15, 2012 21:06
Show Gist options
  • Save brainopia/1839012 to your computer and use it in GitHub Desktop.
Save brainopia/1839012 to your computer and use it in GitHub Desktop.
# 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