This answer does not use a real method as asked, but I didn't need to return a Proc
or change A
. This is a DSL, def_b
should have a meaningful name to the domain, like configure
, and it is more likely to be defined in a module or base class.
class B
class << self
def def_b(&block)
(@b_blocks ||= []) << block
end
def run
return if @b_blocks.nil?
a = A.new
@b_blocks.each { |block| a.instance_eval(&block) }
end
end
def_b do
a
end
end
And it accepts multiple definitions. It could be made accept only a single definition like this:
class B
class << self
def def_b(&block)
raise "b defined twice!" unless @b_block.nil?
@b_block = block
end
def run
A.new.instance_eval(&@b_block) unless @b_block.nil?
end
end
def_b do
a
end
end