# "once" meta-method without "module\_eval" (Pickaxe)

**URL:** <https://www.ruby-forum.com/t/once-meta-method-without-module-eval-pickaxe/120154>\
**Category:** Ruby\
**Created:** [November 14, 2007, 7:23am UTC](https://www.ruby-forum.com/t/once-meta-method-without-module-eval-pickaxe/120154 "2007-11-14T07:23:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 14, 2007, 7:23am UTC](https://www.ruby-forum.com/t/once-meta-method-without-module-eval-pickaxe/120154/1 "2007-11-14T07:23:37Z")

</div>

My impression is that Ruby meta-programming used to lean heavily on  
evaluating strings as code, but is moving away from that. (Good!) In  
that spirit, it would be nice to replace this example from Chapter 24,  
Classes and Objects:

class Date  
class \<\<self  
def once(\*ids) # :nodoc:  
for id in ids  
module\_eval \<\<-“end;”  
alias\_method : **#{id.to\_i}** , :#{id.to\_s}  
private : **#{id.to\_i}**  
def #{id.to\_s}(\*args, &block)  
(@ **#{id.to\_i}** ||= [**#{id.to\_i}** (\*args, &block)])[0]  
end  
end;  
end  
end  
end  
…  
once :as\_string, :as\_YMD  
end

“once” replaces a specified method with a method that caches the return  
value on the first call.

The example above served to illustrate the use of class instance  
variables. However, the replacement that comes to mind does not:

class \<\<self  
def once(\*ids)  
values = {}  
for id in ids  
new\_id = “ **#{id.to\_i}** ”.to\_sym  
alias\_method new\_id, id  
private new\_id  
define\_method (id) do |\*args|  
(values[id] ||= [method(new\_id).call(\*args)])[0]  
end  
end  
end  
end

Is there some way to avoid evals and still use class instance variables?

Is there some way to avoid evals and still handle the “&block” argument?

Is there a natural alternative to the local hash variable?

Am I right to think that eval-ing strings is retro?

(By the way, neither implementation allows “once” to be called twice  
with the same symbol.)

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 14, 2007, 12:46pm UTC](https://www.ruby-forum.com/t/once-meta-method-without-module-eval-pickaxe/120154/2 "2007-11-14T12:46:11Z")

</div>

On Nov 14, 1:23 am, Greg W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> alias_method : __#{id.to_i}__ , :#{id.to_s}
> 
> ```
> 
> end  
> for id in ids  
> Is there some way to avoid evals and still use class instance variables?  
> instance\_variable\_get/set

> Is there some way to avoid evals and still handle the “&block” argument?

Wait for 1.9.

> Is there a natural alternative to the local hash variable?

instance\_variable\_get/set if you want to do it like the original, but  
your local might be better.

> Am I right to think that eval-ing strings is retro?

I’m not sure there’s anything really wrong with eval. Often the code  
looks cleaner compared to these long winded meta-methods.

T.

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 14, 2007, 1:24pm UTC](https://www.ruby-forum.com/t/once-meta-method-without-module-eval-pickaxe/120154/3 "2007-11-14T13:24:55Z")

</div>

Greg W. wrote:

> My impression is that Ruby meta-programming used to lean heavily on  
> evaluating strings as code, but is moving away from that. (Good!)

I have just one question: why is that good? Is it just that avoiding  
evaluation of strings somehow “feels cleaner” or is there actually a  
justification for that?

If you rememer that load/require basically has the effect of eval’ing  
the content of a file, string eval doesn’t feel so evil anymore.

Daniel

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 14, 2007, 6:38pm UTC](https://www.ruby-forum.com/t/once-meta-method-without-module-eval-pickaxe/120154/4 "2007-11-14T18:38:48Z")

</div>

On Nov 14, 2007, at 5:24 AM, Daniel DeLorme wrote:

> If you rememer that load/require basically has the effect of  
> eval’ing the content of a file, string eval doesn’t feel so evil  
> anymore.

not to mention it doesn’t create a closure/leak…

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 14, 2007, 7:08pm UTC](https://www.ruby-forum.com/t/once-meta-method-without-module-eval-pickaxe/120154/5 "2007-11-14T19:08:04Z")

</div>

On Nov 14, 12:37 pm, “ara.t.howard” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Nov 14, 2007, at 5:24 AM, Daniel DeLorme wrote:
> 
> > If you rememer that load/require basically has the effect of  
> > eval’ing the content of a file, string eval doesn’t feel so evil  
> > anymore.
> 
> not to mention it doesn’t create a closure/leak…

another good point! and i still think there should be a way to shut  
out the closure for a block.

T.

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 14, 2007, 9:11pm UTC](https://www.ruby-forum.com/t/once-meta-method-without-module-eval-pickaxe/120154/6 "2007-11-14T21:11:17Z")

</div>

On Nov 13, 11:23 pm, Greg W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Am I right to think that eval-ing strings is retro?

My first dynamically-typed, runtime-interpreted scripting language was  
JavaScript. In JavaScript, eval is considered a bad idea. Loading a  
string of JavaScript code compiles it to bytecode and then executes  
the byte code. Hitting an eval() call in the middle of a program  
requires booting up the lexer and bytecode compiler again, to handle  
that one string, before it can be run. It’s inefficient, and almost  
never needed.

I gather the same is not true in Ruby currently. I think (but may be  
horribly wrong) that this is not so much because of some excellent  
implementation of eval, but a because everything is in the same slow  
pool of interpretation.

I’m totally guessing, but I would suspect that at some point in Ruby’s  
runtime future, startup optimizations/compilations will happen once  
only. If this is true (and I know very little about virtual machines  
and bytecodes and the actual implementation, so I could be wrong),  
then I would suspect that eval of a string (not a block) will incur  
undesirable overhead.

For these reasons (my history with eval in JS and my suspicion that it  
may become undesirable from a performance standpoint) I personally  
never eval strings.

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 14, 2007, 8:39pm UTC](https://www.ruby-forum.com/t/once-meta-method-without-module-eval-pickaxe/120154/7 "2007-11-14T20:39:28Z")

</div>

First, thanks (you all) for the info I asked for.

> > Am I right to think that eval-ing strings is retro?
> 
> I’m not sure there’s anything really wrong with eval. Often the code  
> looks cleaner compared to these long winded meta-methods.

My intuition is that a good argument could be given. But I’m not the  
person to give it.

> not to mention it doesn’t create a closure/leak…

The fact that I can use “define-method” to _almost but not quite_  
achieve the affect of a “def” is almost certainly a Ruby wart. If I run  
into trouble I’ll just blame Matz 🙂
