I have the following Ruby classes:
class Sandwich
class << self
def prepare_with(special_ingredient, &block)
end
def fry!(opts= {})
end
def add_mayo(opts = {})
end
end
end
class Hamburger < Sandwich
end
=> Hamburger.prepare_with(bacon) do
=> Hamburger.fry!
=> Hamburger.add_mayo
=> end
I want to change the call to all methods of the class Hamburgerand add an extra key=>valueto the last Hash parameter.
In Sandwich.prepare_withit is necessary to perform special magic to invoke all Sandwich methods (and all its descendants), for example call, but simply .fry!how .fry!({:ingredient=>special_ingredient}).
EDITED : an additional point, ideally we need to filter out the call to the internal block code, for example, the following code can throw an exception for any prepare_with code that does not handle the methods that call it with an additional parameter:
=> Hamburger.prepare_with(bacon) do
=> Hamburger.fry!
=> h = Hash.new("Go fish")
=> Hamburger.add_mayo
=> end
source
share