在调用方法之前或之后调用特定方法
我正在使用 ruby 1.9.2-p136
我想做的是,创建两个方法: before_method 和 after_method
当我在类中执行此操作时:
before_method :foo, :bar
它应该运行这些任何被调用方法之前的两个方法,即如果我调用类方法 foo1,则这些方法的执行顺序应为: 富 酒吧 foo1
另外,我想要一个与 before_method 相反的方法 after_method
任何人都可以告诉如何去做吗?
提前致谢 :)
I'm using ruby 1.9.2-p136
What i want to do is, create two methods: before_method and after_method
When i do this in a class:
before_method :foo, :bar
it should run these two methods before any called method, i.e. if i call a class method foo1, the order in which the methods should be executed is:
foo
bar
foo1
Also, i want a method after_method which does the opposite of before_method
Can anyone tell how to go about it??
Thanks in advance :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您想为 ActiveRecord 模型执行此操作吗?或者只是通用类?因为我编写了一个 Rails 插件,它将
before_filter
和after_filter
方法添加到 ActiveRecord 模型中。也许您可以使用它,或者查看代码并从那里开始: https://github.com/deleteman /过滤模型
希望有帮助!
如果您有疑问,请随时提问:)
Are you looking to do this for ActiveRecord Models? Or just generic classes? Because I've written a rails plugin that adds
before_filter
andafter_filter
methods to the ActiveRecord Models.Maybe you can use it, or look at the code and go from there: https://github.com/deleteman/Filtered-Models
Hope that helps!
If you have questions, feel free to ask :)