-1

你如何用红宝石制作过滤器?我知道该函数已经存在于 rails 中,但我们如何在 ruby​​ 程序中对其进行编码?这是程序:

module Filter
  def before_filter *args   
  end
  def after_filter *args  
  end 
end  

class Ingredient  
  def one
    puts "in one"
  end

  def two
    puts "in two"
  end

  def three
    puts "in three"
  end

  def four
    puts "in four"
  end

  extend Filter

  before_filter :one, :two 
  after_filter :four
end

dish1 = Ingredient.new 
dish1.three
4

1 回答 1

1

Rails 可以避免这种情况,因为它们控制方法的调用方式。但是在这里,您是直接从外部呼叫三个。

为了适应您提出的限制,任何这样做的代码都将是完全怪诞的。另外,后过滤器有什么作用?它确定返回值吗?

我已经实现了与此类似的东西,虽然界面略有不同,可以节省一些怪诞,但仍然很糟糕。https://gist.github.com/3450271

此外,还有一个明显的问题是为什么你需要这样的东西。

于 2012-09-07T07:42:10.493 回答