Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Python-like decorators can be implemented in Ruby. I won't try to explain and give examples, because Yehuda Katz has already published a good blog post about decorators DSL in Ruby, so I highly recommend to read it:</p> <ul> <li><a href="http://yehudakatz.com/2009/07/11/python-decorators-in-ruby/" rel="noreferrer">Python Decorators in Ruby</a></li> <li><a href="http://github.com/wycats/ruby_decorators" rel="noreferrer">Source code and tests</a></li> </ul> <p>UPDATE: I've got a couple of vote downs on this one, so let me explain further.</p> <p><code>alias_method (and alias_method_chain)</code> is NOT exactly the same concept as a decorator. It is just a way to re-define method implementation without using inheritance (so client code won't notice a difference, still using the same method call). It could be useful. But also it could be error-prone. Anyone who used Gettext library for Ruby probably noticed that its ActiveRecord integration has been broken with each Rails major upgrade, because aliased version has been following the semantics of an old method. </p> <p>The purpose of a decorator in general is NOT to change the internals of any given method and still be able to call the original one from a modified version, but to enhance the function behavior. The "entry/exit" use case, which is somewhat close to <code>alias_method_chain</code>, is only a simple demonstration. Another, more useful kind of a decorator could be <code>@login_required</code>, which checks authorization, and only runs the function if authorization was successful, or <code>@trace(arg1, arg2, arg3)</code>, which could perform a set of tracing procedures (and be called with different arguments for different methods decoration).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload