在我的程序中,我使用了状态机,并且有许多方便的方法。我目前正在创建一长串“?”模型中的方法。
def purchase_ready?
self.current_state == 'purchase_ready'
end
def completed?
self.current_state == 'completed'
end
def region_prepared?
self.current_state == 'region_prepared'
end
执行此操作的元编程方法是什么?
最佳答案
...这是一个答案!
感谢这个博客:http://rohitrox.github.io/2013/07/02/ruby-dynamic-methods/
[:purchase_ready, :completed, :region_prepared].each do |method|
define_method "#{method}?" do
self.current_state == "#{method}"
end
end
关于ruby - 元编程 Ruby 便捷方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29352824/