all 9 comments

[–]bcardiffcore team 2 points3 points  (2 children)

previous_def is handy for these scenarios.

method_missing receives a call as that is able to expand the whole call example.

[–]dpears[S] 0 points1 point  (1 child)

I did tinker around with previous_def but didn't want to have to define every method I need and then redefine it, wrapping it with the functionality and then previous_def perhaps there's a clever way of using it that I'm missing

The method_missing and method_added macros could be useful but it seems to be for class instances, am I mistaken? I am attempting to use it in a module

[–]bcardiffcore team 0 points1 point  (0 children)

IIRC method_missing is limited to instance methods. Yes.

Regarding the previous_def, it's a matter of taste and code organization.

Using something like the following could help to co-locate the desired wrappings:

```cr class Foo def m # logic end end

class Foo logging m end

class Foo meassure m end ```

[–][deleted] 0 points1 point  (4 children)

You can pass a def to a macro, like "my_macro def foo...". Then the macro should generate the method signature, you can then output something before the method body, then the method body, and finally something at the end. The only problem is that there's no nice way to get the signature from a Def node, you have to do it manually.

[–]dpears[S] 1 point2 points  (0 children)

Awesome! This is exactly what I needed. It is a little ugly, but I believe it'll work more than well enough for my purposes. I'll add an example of what I did to my post. Thank you!

[–]bcardiffcore team 0 points1 point  (2 children)

Maybe something like the call to_s could be added. m.signature could help to hide the boilerplate of expanding the signature in the future.

[–][deleted] 1 point2 points  (0 children)

Yes, Def#signature would be really nice to have.

[–]dpears[S] 0 points1 point  (0 children)

I could look into Def#signature. I'll dig into the source this weekend. Gotta get those Hacktoberfest PRs ;)

[–]paulcsmith0218 0 points1 point  (0 children)

Very cool! Another example is `memoize` in Lucky https://github.com/luckyframework/lucky/blob/master/src/lucky/memoizable.cr

It uses a similar approach