# Meta Programming Question

**URL:** <https://www.ruby-forum.com/t/meta-programming-question/131021>\
**Category:** Ruby\
**Created:** [March 5, 2008, 8:55am UTC](https://www.ruby-forum.com/t/meta-programming-question/131021 "2008-03-05T08:55:50Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![MohsinHijazee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e68b1a/32.png) [@MohsinHijazee](https://www.ruby-forum.com/u/MohsinHijazee)\
**Post date:** [March 5, 2008, 8:55am UTC](https://www.ruby-forum.com/t/meta-programming-question/131021/1 "2008-03-05T08:55:50Z")

</div>

Hello!

Consider this case:

myHash = {:a\_line\_of\_code =\> “pre\_count = Posts.count”,  
:another\_line =\> %q~message =  
get\_formatted\_message(#{pre\_count})~}

If I have a hash like this which contains code snippets as strings,  
How can I execute them? Moreover consider also that the second code  
snippet uses the value of the variable assigned in the first one.

Regards,  
Mohsin

---

<div class="post-metadata">

**Author:** ![MohsinHijazee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e68b1a/32.png) [@MohsinHijazee](https://www.ruby-forum.com/u/MohsinHijazee)\
**Post date:** [March 5, 2008, 9:21am UTC](https://www.ruby-forum.com/t/meta-programming-question/131021/2 "2008-03-05T09:21:13Z")

</div>

On Mar 5, 1:51 am, MohsinHijazee [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> snippet uses the value of the variable assigned in the first one.
> 
> Regards,  
> Mohsin

If you need to use a variable from a hash maybe you should be using it  
outside the hash context. What I mean is that probably you shouldn’t  
be putting a variable there.  
You can always use eval to execute strings, but may not be a great  
idea.

In any case, you may use procs instead of storing variables if what  
you want is dynamically generated values for the hash.

> > hash = {:something =\> proc{ Posts.count }, :other\_thing =\> proc{|var| do\_something\_with(var)}}

and you could call

> > hash[:other\_thing][hash[:something]]

but the ugliness of that cries desperately for a more elegant way. If  
you need to execute code, I think there is a feature or Ruby called  
errr, methods.

Could you give more details of what you are trying?

---

<div class="post-metadata">

**Author:** ![MohsinHijazee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e68b1a/32.png) [@MohsinHijazee](https://www.ruby-forum.com/u/MohsinHijazee)\
**Post date:** [March 5, 2008, 11:35am UTC](https://www.ruby-forum.com/t/meta-programming-question/131021/3 "2008-03-05T11:35:46Z")

</div>

On Mar 5, 1:19 pm, Rubén Medellín [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > If I have a hash like this which contains code snippets as strings,  
> > idea.  
> > but the ugliness of that cries desperately for a more elegant way. If  
> > you need to execute code, I think there is a feature or Ruby called  
> > errr, methods.
> 
> Could you give more details of what you are trying?

I am actually trying to automat the testing of an existing Rails  
application  
to the point where I only specify that what function to be tested with  
what  
params and what to expect in the response. Some of the functions being  
tested  
change the state of the system that’s why its necessary to calcuate  
some of  
the system stats before executing the test call to that function and  
after the  
test call and then to check the before and after responses to validate  
that  
the call really made the change to the system. Here is the structure  
(yet rough) to  
list all the tests in the system which would be automatically  
executed.

# This is hash and it would be iterated

mytests = {

:test\_delete\_with\_wrong\_id =\>  
{  
:method =\> :get  
# This before would be executed befoer calling the above  
mention method  
:before =\> “json = Customer.find(1).to\_json”  
:url =\> [‘/databases/:database\_id/entities/:id.format’, 34, 56]  
:after \>= “json = @response.body”  
:params =\> {}  
:session =\> {‘user’ =\> user}  
# Response should be succuss  
:response =\> :success  
# Each of the assertions in this array would be executed  
:assertions =\> [“assert json, json”]  
}

}

---

<div class="post-metadata">

**Author:** ![MohsinHijazee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e68b1a/32.png) [@MohsinHijazee](https://www.ruby-forum.com/u/MohsinHijazee)\
**Post date:** [March 5, 2008, 4:52pm UTC](https://www.ruby-forum.com/t/meta-programming-question/131021/4 "2008-03-05T16:52:14Z")

</div>

On Wed, Mar 5, 2008 at 2:35 AM, MohsinHijazee [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > > get\_formatted\_message(#{pre\_count})~}  
> > > be putting a variable there.
> > > 
> > > > hash[:other\_thing][hash[:something]]  
> > > > what  
> > > > list all the tests in the system which would be automatically  
> > > > executed.

Is there a reason that Test::Unit (with appropriate setup and teardown  
for  
the test cases) won’t work? If it does, that would be better than  
reinventing  
the wheel. If it doesn’t, knowing what limitation you hit with that  
might  
help people propose an appropriate solution.

---

<div class="post-metadata">

**Author:** ![MohsinHijazee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e68b1a/32.png) [@MohsinHijazee](https://www.ruby-forum.com/u/MohsinHijazee)\
**Post date:** [March 6, 2008, 1:35am UTC](https://www.ruby-forum.com/t/meta-programming-question/131021/5 "2008-03-06T01:35:22Z")

</div>

If you are testing and want to verify that a method has been called with  
the appropriate parameters, check out a library like FlexMock  
([http://onestepback.org/software/flexmock/](http://onestepback.org/software/flexmock/))

---

<div class="post-metadata">

**Author:** ![MohsinHijazee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e68b1a/32.png) [@MohsinHijazee](https://www.ruby-forum.com/u/MohsinHijazee)\
**Post date:** [March 7, 2008, 8:38am UTC](https://www.ruby-forum.com/t/meta-programming-question/131021/6 "2008-03-07T08:38:04Z")

</div>

2008/3/5, MohsinHijazee [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> If I have a hash like this which contains code snippets as strings,  
> How can I execute them? Moreover consider also that the second code  
> snippet uses the value of the variable assigned in the first one.

Mohsin, as others have mentioned you should probably use other  
libraries, but just in case you want to continue to eval strings,  
there’s no problem with assigning variables and using them later:

irb(main):001:0\> eval “var = 123”  
=\> 123  
irb(main):002:0\> eval “2 \* var”  
=\> 246  
irb(main):003:0\>

Regards,  
Pit

---

<div class="post-metadata">

**Author:** ![MohsinHijazee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e68b1a/32.png) [@MohsinHijazee](https://www.ruby-forum.com/u/MohsinHijazee)\
**Post date:** [March 7, 2008, 4:11pm UTC](https://www.ruby-forum.com/t/meta-programming-question/131021/7 "2008-03-07T16:11:28Z")

</div>

On Fri, Mar 7, 2008 at 8:37 AM, Pit C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> irb(main):001:0\> eval “var = 123”  
> =\> 123  
> irb(main):002:0\> eval “2 \* var”  
> =\> 246  
> irb(main):003:0\>
> 
> Regards,  
> Pit

This does not really serve a lot as var is visible in the eval binding  
_only_.  
E.g.  
puts var # will not output 123, not even 42 😉

Cheers  
Robert

–  
[http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)

* * *

Whereof one cannot speak, thereof one must be silent.  
Ludwig Wittgenstein

---

<div class="post-metadata">

**Author:** ![MohsinHijazee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e68b1a/32.png) [@MohsinHijazee](https://www.ruby-forum.com/u/MohsinHijazee)\
**Post date:** [March 7, 2008, 4:34pm UTC](https://www.ruby-forum.com/t/meta-programming-question/131021/8 "2008-03-07T16:34:08Z")

</div>

2008/3/7, Robert D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> This does not really serve a lot as var is visible in the eval binding _only_.

Where have you got the requirement from, that those variables should  
be visible outside of the eval code?

Regards,  
Pit
