January 2024
Intermediate to advanced
718 pages
20h 15m
English
Let’s bring together all the metaprogramming topics we’ve discussed in a final example by writing a module that allows us to trace the execution of methods in any class that mixes the module in. This would let us write the following:
| | require_relative "trace_calls" |
| | |
| | class Example |
| | def one(arg) |
| | puts "One called with #{arg}" |
| | end |
| | end |
| | |
| | ex1 = Example.new |
| | ex1.one("Hello") # no tracing from this call |
| | |
| | class Example |
| | include TraceCalls |
| | def two(arg1, arg2) |
| | arg1 + arg2 |
| | end |
| | end |
| | |
| | ex1.one("Goodbye") # but we see tracing from these two |
| | puts ex1.two(4, 5) |
Produces:
| | One called with Hello |
| | ==> calling one with ["Goodbye"] |
| |
Read now
Unlock full access