在调用方法之前或之后调用特定方法

发布于 2024-12-22 05:03:31 字数 409 浏览 1 评论 0原文

我正在使用 ruby​​ 1.9.2-p136

我想做的是,创建两个方法: before_methodafter_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

路弥 2024-12-29 05:03:31

您想为 ActiveRecord 模型执行此操作吗?或者只是通用类?因为我编写了一个 Rails 插件,它将 before_filterafter_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 and after_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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文