Prev | Current Page 87 | Next

Brad Ediger

"Advanced Rails"

It can be
replaced to modify the default behavior. By default, in the test environment, deprecation
warnings print to the standard error stream. In development, they go to the logger. In
production, deprecation warnings are silenced.
Deprecated instance variables
ActionController defines a set of objects that are made available to controllers. These
objects used to be publicly available instance variables; this usage is now deprecated. For
example, the session object (available to controllers through the session and session=
methods) used to be accessible as @session. This creates a problem: how do we intercept
access to these deprecated instance variables? Ruby isn??™t so helpful as to provide us a hook
that informs us upon instance variable access.
Rails uses a neat trick: a proxy class. The protected objects were moved out of those
instance variables and moved into ???internal??? instance variables (which begin with an
underscore). The old instance variables were replaced with instances of ActiveSupport::
Deprecation::DeprecatedInstanceVariableProxy, which proxies to the real objects. When a
method is called on these proxies (such as @session.id), a warning is generated before delegating
the call to the real object. This is a common Ruby trick that is used in several other
places in Rails??”replacing a standard object with a proxy object that responds in a special
way to certain methods.


Pages:
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99