How can I intercept a method call in Boo?
Asked Answered
E

1

4

Ruby has method_missing , Python has getattr. Does Boo offer something I can use to intercept method calls?

E answered 18/7, 2009 at 14:13 Comment(0)
E
7

Yes, Boo has IQuackFu.

Basically, you implement IQuackFu, which has three methods:

  • QuackGet: gets called when you get a property value
  • QuackSet gets called when you set a property value
  • QuackInvoke: gets called when you invoke a method

Here's an example.

Excruciate answered 18/7, 2009 at 15:27 Comment(3)
A small question: can Boo intercept other object's method invocations? Kinda like Groovy does with the metaclass. I'm thinking the answer's no, since it's statically typed, but I'd like to be sure.E
I don't think you can do it the same way as Groovy, but you could use an IQuackFu-enabled class to wrap/decorate another class. Your IQuackFu-enabled class could have a dictionary of methods and properties to implement expando-behavior.Excruciate
Boo isn't truly dynamic like groovy, you can't change the definition of a Type at runtime. If you declare a variable as 'duck' it essentially does compiler magic to turn that into late binding against that object, which will flow through IQuackFu if it's available. There is no actual metaclass in Boo. You would need IronRuby or IronPython to get something more like the metaclass.Skylight

© 2022 - 2024 — McMap. All rights reserved.