要開發(fā)plug,那么rails的版本必須是0.14后的。
plug的基本原理是什么呢?我們看一個例子:
我們假想這是rails的一個核心類
class A
?def foo
??"foo"
?end
end
這個就是你的插件了,你可以新增方法或者是覆蓋原來的方法。
class A
?def foo
??"foofoo"
?end
?
?def bar
??"bar"
?end
end
現(xiàn)在可以在rails應用程序中使用A了,你的插件已經神奇般添加
進了Rails的核心了。
a = A.new
puts a.foo???????? #"foofoo"
puts a.bar???????? #"bar"
其實這用到的是ruby類的不封閉原則。