Last active
August 29, 2015 14:08
-
-
Save stackdump/1faaea114e714552e428 to your computer and use it in GitHub Desktop.
metaclass / DSL + inheritance
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
require 'pp' | |
module Base | |
def metaclass | |
class << self | |
self | |
end | |
end | |
def p_r | |
[] # base case | |
end | |
def add(x) | |
#p "level => #{self.to_s}" | |
metaclass.instance_eval do | |
define_method(:p_r) { | |
pp caller.first | |
puts "#{x} / recurse" | |
dat = super() | |
puts "#{x} / concat #{dat.inspect}" | |
[x].concat dat | |
} | |
end | |
end | |
end | |
class Baz | |
extend Base | |
add :baz | |
end | |
class Foo < Baz | |
add :foo | |
end | |
class Bar < Foo | |
add :bar | |
end | |
p Foo.p_r | |
# => [:foo, :baz] | |
p Bar.p_r | |
# => [:bar, :foo, :baz] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment