Skip to content

Instantly share code, notes, and snippets.

@newtonapple
Created July 8, 2010 11:09
Show Gist options
  • Save newtonapple/467887 to your computer and use it in GitHub Desktop.
Save newtonapple/467887 to your computer and use it in GitHub Desktop.
The evil of including Modules globally. Stop it people!
module A
class C
def initialize
@foo = 'lame'
@boo = 'boo'
end
def bar
foo # wait, foo is not defined in C?! wait for it...
end
def baz
boo # wait boo is not defined in C! wait for it... (hint: private method)
end
end # class C
# a typo.. foo was meant to be in C
def foo
@foo
end
end # module A
# but library decides to pollute Object by including A in global namespace...
include A
# defining foo in Object (Object#foo).
# same as including a Module in global namespace.
# all subclasses of Object now get these methods... including C!
def boo
@boo
end
c = A::C.new
# these still works... awesome?
c.bar # lame
c.baz # boo
class B; end
B.new.respond_to? :foo # => true. wtf?!!
B.new.boo # private method error...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment