3

I`ve got the following Ruby classes:

class Sandwich
  class << self
    def prepare_with(special_ingredient, &block)
      # Some very very special magic is done here to
      # call instead just .fry! as .fry!({:ingredient=>special_ingredient})
      # and for any other method the same
    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 modify of calling all methods of Hamburger class and add additional key=>value into last parameter Hash.

Some special magic should be done in Sandwich.prepare_with to call all methods of Sandwich (and all its descendants), like call instead just .fry! as .fry!({:ingredient=>special_ingredient}).

EDITED: Additional point that ideally we need to filter calling of inside block code, for example following code would raise exception for any prepare_with code, that doesn`t filter methods it calling with additional parameter:

=> Hamburger.prepare_with(bacon) do
=>   Hamburger.fry!
=>   h = Hash.new("Go fish") 
=>   Hamburger.add_mayo
=> end
4

2 に答える 2

3

インスタンスメソッドでfry!はないのはなぜですか?add_mayo

編集:質問投稿者が要求したように、インスタンスメソッドなし:

class Sandwich
  class << self
    def prepare_with(special_ingredient, &block)
      @@default_opts = {:special_ingredient => special_ingredient}
      block.call
    end

    def fry!(opts={})
      opts = opts.merge(@@default_opts)
      puts "fried with #{opts[:special_ingredient]}"
    end

    def add_mayo(opts = {})
      puts "added mayo"
    end
  end
end

class Hamburger < Sandwich
end

Hamburger.prepare_with(:bacon) do 
  Hamburger.fry!
  Hamburger.add_mayo
end

Hamburger.prepare_with(:tofu) do 
  Hamburger.fry!
end

出力:

fried with bacon
added mayo
fried with tofu
于 2012-04-24T12:38:04.813 に答える
0

簡潔な答え

block.call :ingredient => special_ingredient

長い答え

オブジェクトを扱う必要があると思います。add_mayo と fly は、クラス メソッドではなくインスタンス メソッドにする必要があります。

私は次のようなものを見るでしょう

class Sandwich
  class << self
    def prepare &block
      sandwich = self.new
      block.call sandwich
      sandwich
    end
  end

  def fry(opts = {})
    #stuff
  end

  def add_mayo(opts = {})
  end
end

class Hamburger < Sandwich; end

hamburger = Hamburger.prepare do |h|
  h.fry :ingredient => :bacon
  h.add_mayo
end
于 2012-04-24T12:33:56.693 に答える