Skip to content

Instantly share code, notes, and snippets.

@stackdump
Last active August 29, 2015 14:08
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 stackdump/1faaea114e714552e428 to your computer and use it in GitHub Desktop.
Save stackdump/1faaea114e714552e428 to your computer and use it in GitHub Desktop.
metaclass / DSL + inheritance
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