devolución de llamada de Ruby cuando cambia el estado
Frecuentes
Visto 179 veces
4
Is there a way to provide similar functionality in ruby to what añadir reloj does in clojure? I am looking for a way to register a block/proc/lambda as a callback on a data structure, say a hash, and be called whenever the state of the hash changes.
2 Respuestas
2
Yes, you can do this in Ruby, but you'll need to build up the infrastructure yourself or find a library that does it.
Porque Ruby's "duck" typing, a method that expects a Hash instance can be given a class instance that looks, feels, and sounds like a hash. The receiving method can just treat the object as a hash and everything will be fine.
Mientras tanto, your other software will know that it is really an ObservableHash
(or whatever you decide to call it.) And you will have registered your callback-on-change methods, etc.
Aquí hay una blog that will help get you started on creating your ObservableHash
.
A final tip: be sure to implement todos of the Hash methods. That way the other methods that receive it won't be able to tell the difference between an observable_hash and a hash.
It is my understanding that the Rails ActionController#params
"hash" is, in fact, a class instance that seems to be a hash (but really isn't). Look up its source to check it out.
Respondido 28 ago 12, 14:08
Thanks @JonasElfström -- very helpful. - larry k
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas ruby clojure or haz tu propia pregunta.
yes, there is a way to provide such functionality as evident in Activerecord::Observer - nurettin