如何在 Ruby 中进行事件?
我有 C# 背景,刚刚开始使用 Ruby 编程。 问题是,我需要知道如何在类中引发事件,以便在需要发生事情时可以触发各种观察者。
问题是我关于 Ruby 的书籍甚至没有提到事件,更不用说提供示例了。 有人能帮助我吗?
I come from a C# background, and have just started programming in Ruby. The thing is, that I need to know how I can raise events in my classes so that various observers can be triggered when things need to happen.
The problem is the books I have on Ruby don't even mention events, let alone provide examples. Is anyone able to help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
问题已经得到解答,但是有一个观察者如果您想看看的话,它就内置在标准库中。 我过去曾在一个小型游戏项目中使用过它,效果非常好。
The question has already been answered, but there's an observer built right into the standard library if you want to give that a look. I've used it in the past for a small game project, and it works very well.
极其简单的 Ruby 监听器。 这并不完全是 .NET 事件的替代品,但这是一个非常简单的侦听器的极其简单的示例。
使用:
输出:
Extremely simple Ruby listener. This is not exactly a replacement for .NET events, but this one is an extremely simple example of a very simple listener.
To use:
Output:
我尝试用 Ruby 编写一个 GUI 库,并使用一点 C,主要是 Ruby。 结果太慢了,我放弃了,再也没有释放它。 但我为它编写了一个事件系统,试图使其比 C# 的更容易。 我重写了几次以使其更易于使用。 我希望它有所帮助。
I tried writing a GUI library in Ruby with a little C and primarily Ruby. It ended up being so slow I gave up and never released it. But I wrote an event system for it that I tried to make easier than C#'s. I rewrote it a couple times to make it easier to use. I hope it is somewhat helpful.
披露:我是
event_aggregator
gem 的维护者,具体取决于您想要如何解决问题,您可能会使用事件聚合器。 通过这种方式,您可以发布某种类型的消息,然后让您的对象侦听您希望它们接收的类型。 在某些情况下,这可能比正常事件更好,因为对象之间的耦合非常松散。 事件生成者和侦听器不需要共享彼此的引用。
有一个名为
event_aggregator
的 gem 可以帮助您完成此任务。 使用它,您可以执行以下操作:请注意,默认情况下它是异步的,因此如果您仅执行此脚本,则脚本可能会在事件传递之前退出。 要禁用异步行为,请使用:
希望这对您的情况有所帮助
Disclosure: I am the maintainer of the
event_aggregator
gemDepending on how you want to approach the problem you could potentially use an event aggregator. This way you can publish messages of a certain type and then have your objects listen to the types you want them to receive. This can in certain cases be better than normal events because you get a very loose coupling between your objects. The event producer and listener does not need to share a reference to the other.
There is a gem that helps you with this called
event_aggregator
. With it you can do the following:Be aware that it is async by default, so if you execute just this script the script might exit before the events are passed. To disable async behaviour use:
Hopefully this can be helpful in your case
我不确定你的意思到底是什么,但你可能可以在你的类中使用异常并在某些“事件”上引发它们。 如果您需要事件进行 GUI 开发,那么大多数 GUI 框架都会定义自己的事件处理样式。
希望这能在一定程度上回答您的问题。
I'm not sure of exactly what you mean but you could probably use exceptions in your classes and raise them on certain "events". If you need event for GUI development then most GUI frameworks define their own event handling style.
Hope this somewhat answers you're question.