On Dec 27, 2006, at 14:26, ara.t.howard / noaa.gov wrote:
> On Thu, 28 Dec 2006, Eric Hodel wrote:
>
>> I already did this:
>>
>> http://blog.segment7.net/articles/2006/03/06/attr-vs-method-vs- 
>> define_method
>>
>> You can probably match def for speed by using one of the eval  
>> methods.
>
> did you mean to say 'evil methods'  ;-)

There's no need to use the evil eval:

$ parse_tree_show
class X; class_eval { def y() 5; end } end
[[:class,
   :X,
   [:const, :Object],
   [:defn, :y, [:scope, [:block, [:args], [:lit, 5]]]]]]

Which generates the same AST as a regular def:

$ parse_tree_show
class X; def y() 5; end; end
[[:class,
   :X,
   [:const, :Object],
   [:defn, :y, [:scope, [:block, [:args], [:lit, 5]]]]]]

If you use alias, the aliased method will be slightly slower:

$ parse_tree_show
class X; def y(); 5 end; alias x y; end
[[:class,
   :X,
   [:const, :Object],
   [:defn, :x, [:fbody, [:scope, [:block, [:args], [:lit, 5]]]]],
   [:defn, :y, [:scope, [:block, [:args], [:lit, 5]]]]]]

But you can use ruby2ruby to regenerate the aliased method without  
the :fbody node (or, just inline it).  I'll leave this as an exercise  
to the reader.

-- 
Eric Hodel - drbrain / segment7.net - http://blog.segment7.net

I LIT YOUR GEM ON FIRE!