Skip to content

Instantly share code, notes, and snippets.

@enricostano
Created May 22, 2014 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enricostano/c367aac670b574af93e0 to your computer and use it in GitHub Desktop.
Save enricostano/c367aac670b574af93e0 to your computer and use it in GitHub Desktop.
module Alfa
def self.included(base)
puts "#{self} included in #{base}"
unless base.const_defined?(:PAPPA)
base.const_set :PAPPA, [:a, :b]
end
end
def self.extended(base)
puts "#{self} extended in #{base}"
unless base.const_defined?(:PAPPA)
base.const_set :PAPPA, [:a, :b]
end
end
def papa
p self::PAPPA
end
end
module Beta
PAPPA = [1, 2]
extend Alfa
def self.hola
papa
end
end
class Teta
include Alfa
def initialize()
# just to keep in mind that this is a class that we'll instantiate
end
def hola
papa
end
end
puts "from Beta #{Beta.hola}"
puts "from Teta #{Teta.new().hola}"
@enricostano
Copy link
Author

This is what I get:

Alfa extended in Beta
Alfa included in Teta
[1, 2]
from Beta [1, 2]
puppa.rb:17:in `papa': #<Teta:0x007f9b30b27028> is not a class/module (TypeError)
    from puppa.rb:36:in `hola'
    from puppa.rb:41:in `<main>'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment