[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Meta Programming Question

MohsinHijazee

3/5/2008 7:51:00 AM

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
7 Answers

Rubén Medellín

3/5/2008 8:19:00 AM

0

On Mar 5, 1:51 am, MohsinHijazee <mohsinhija...@gmail.com> wrote:
> 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

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?

MohsinHijazee

3/5/2008 10:34:00 AM

0

On Mar 5, 1:19 pm, Rubén Medellín <CHub...@gmail.com> wrote:
> On Mar 5, 1:51 am, MohsinHijazee <mohsinhija...@gmail.com> wrote:
>
> > 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
>
> 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?

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"]
}


}

Christopher Dicely

3/5/2008 3:51:00 PM

0

On Wed, Mar 5, 2008 at 2:35 AM, MohsinHijazee <mohsinhijazee@gmail.com> wro=
te:
>
> On Mar 5, 1:19 pm, Rub=E9n Medell=EDn <CHub...@gmail.com> wrote:
> > On Mar 5, 1:51 am, MohsinHijazee <mohsinhija...@gmail.com> wrote:
> >
> > > Hello!
> >
> > > Consider this case:
> >
> > > myHash =3D {:a_line_of_code =3D> "pre_count =3D Posts.count",
> > > :another_line =3D> %q~message =3D
> > > 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
> >
> > 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 =3D {:something =3D> proc{ Posts.count }, :other_thing =3D> pr=
oc{|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?
>
> 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.

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 reinventi=
ng
the wheel. If it doesn't, knowing what limitation you hit with that might
help people propose an appropriate solution.

Paul McMahon

3/6/2008 12:34:00 AM

0

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...)


Pit Capitain

3/7/2008 7:37:00 AM

0

2008/3/5, MohsinHijazee <mohsinhijazee@gmail.com>:
> 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

Robert Dober

3/7/2008 3:11:00 PM

0

On Fri, Mar 7, 2008 at 8:37 AM, Pit Capitain <pit.capitain@gmail.com> wrote:
> 2008/3/5, MohsinHijazee <mohsinhijazee@gmail.com>:
>
> > 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
>
>
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.blo...

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

Pit Capitain

3/7/2008 3:33:00 PM

0

2008/3/7, Robert Dober <robert.dober@gmail.com>:
> 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