# Uneval,mtime,hash - Meta programming

**URL:** <https://www.ruby-forum.com/t/uneval-mtime-hash-meta-programming/77832>\
**Category:** Ruby\
**Created:** [November 30, 2006, 10:33pm UTC](https://www.ruby-forum.com/t/uneval-mtime-hash-meta-programming/77832 "2006-11-30T22:33:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [November 30, 2006, 10:33pm UTC](https://www.ruby-forum.com/t/uneval-mtime-hash-meta-programming/77832/1 "2006-11-30T22:33:34Z")

</div>

I often do things that take far too much time.

So I create “make” like scripts that understand dependencies and  
targets and when things need to be remade or not.

This saves me heaps and heaps of time.

So I just had the idea that I could pull everything into self  
contained ruby if I could tell if a method had changed or not so I  
would know whether I had to rerun it or not.

Hence the Questions:

Q1. Is there an “uneval” in ruby. ie. Give it a method name, return the  
string that, if eval’d would give the method.

Q2. Is there a “method\_hash”, which would change if the method had  
changed.

I suppose I could store the methods as strings, compute an md5sum and  
then ‘eval’ the string. Hmm.

John C. Phone : (64)(3) 358 6639  
Tait Electronics Fax : (64)(3) 359 4632  
PO Box 1645 Christchurch Email : removed\_email\_address@domain.invalid  
New Zealand

---

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [November 30, 2006, 10:51pm UTC](https://www.ruby-forum.com/t/uneval-mtime-hash-meta-programming/77832/2 "2006-11-30T22:51:36Z")

</div>

On Fri, 1 Dec 2006, John C. wrote:

> Hence the Questions:
> 
> Q1. Is there an “uneval” in ruby. ie. Give it a method name, return the  
> string that, if eval’d would give the method.
> 
> Q2. Is there a “method\_hash”, which would change if the method had changed.
> 
> I suppose I could store the methods as strings, compute an md5sum and then  
> ‘eval’ the string. Hmm.

harp:~ \> cat a.rb  
class Module  
def method\_added \*a, &b  
p [a, b]  
end  
end

class C  
def adding\_a\_method() end  
end

harp:~ \> ruby a.rb  
[[:method\_added], nil]  
[[:adding\_a\_method], nil]

now you just need to cache them before they are added.

-a

---

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [November 30, 2006, 11:25pm UTC](https://www.ruby-forum.com/t/uneval-mtime-hash-meta-programming/77832/3 "2006-11-30T23:25:45Z")

</div>

On Fri, 1 Dec 2006, removed\_email\_address@domain.invalid wrote:

> harp:~ \> ruby a.rb  
> [[:method\_added], nil]  
> [[:adding\_a\_method], nil]
> 
> now you just need to cache them before they are added.

The trouble is the methods get “added” every time the script gets  
run. Thus depending “Makefile” like on the mtime isn’t going to work.

I need a way of doing a strong hash of the method “contents” to  
evaluate whether it has changed or not.

The obvious would be an “uneval” or to intercept prior to eval and  
look at the string rep of the method. This has the downside of being  
inappropriately sensitive to white space and comments.

A compile to byte code / interpret byte code system like yarv and  
jruby would have a byte code string that would be perfect.

I think vanilla ruby has an internal ‘tree of nodes’ structure. I  
wonder how to grab that…

John C. Phone : (64)(3) 358 6639  
Tait Electronics Fax : (64)(3) 359 4632  
PO Box 1645 Christchurch Email : removed\_email\_address@domain.invalid  
New Zealand

---

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [November 30, 2006, 11:37pm UTC](https://www.ruby-forum.com/t/uneval-mtime-hash-meta-programming/77832/4 "2006-11-30T23:37:47Z")

</div>

On 11/30/06, John C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I think vanilla ruby has an internal ‘tree of nodes’ structure. I  
> wonder how to grab that…

Try a search for ParseTree and ruby2ruby.
