Skip to content

Instantly share code, notes, and snippets.

@sebastiandeutsch
Created October 5, 2010 03:27
Show Gist options
  • Save sebastiandeutsch/610936 to your computer and use it in GitHub Desktop.
Save sebastiandeutsch/610936 to your computer and use it in GitHub Desktop.
# I wanted to
# define a class which has a foo method
# which can also be called from a block
# so you can spice it and call the super
# class method again
#
# thanks to banisterfiend, xxxxxx and coderr
# for helping me on this one
class Document
def self.foo(test)
puts "foo: #{test}"
end
module TabProxy
def foo(test)
puts "block"
super(test)
end
end
def self.tab( name, &block )
context = eval 'self', block.binding
dup_context = context.dup
dup_context.extend TabProxy
dup_context.instance_eval &block
dup_context.instance_variables.each do |v|
context.instance_variable_set(v, dup_context.instance_variable_get(v))
end
end
end
class Dummy < Document
foo :foo
tab :general do
foo :bar
end
foo :baz
end
# should output
foo: foo
block
foo: bar
foo: baz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment