Prev | Current Page 40 | Next

Brad Ediger

"Advanced Rails"


??? Since the methods being called never actually exist??”they are just intercepted at
the last step of the method lookup process??”they cannot be documented or
introspected as conventional methods can.
??? Because all dynamic methods must go through the method_missing method, the
body of that method can become quite large if there are many different aspects
of the code that need to add methods dynamically.
??? Using method_missing restricts compatibility with future versions of an API.
Once you rely on method_missing to do something interesting with undefined
methods, introducing new methods in a future API version can break your users??™
expectations.
A good alternative is the approach taken by ActiveRecord??™s generate_read_methods
feature. Rather than waiting for method_missing to intercept the calls, ActiveRecord
generates an implementation for the attribute setter and reader methods so that they
can be called via conventional method dispatch.
This is a powerful method in general, and the dynamic nature of Ruby makes it possible
to write methods that replace themselves with optimized versions of themselves
when they are first called. This is used in Rails routing, which needs to be very
fast; we will see that in action later in this chapter.
Generative Programming: Writing Code On-the-Fly
One powerful technique that encompasses some of the others is generative
programming??”code that writes code.


Pages:
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52