Problem with "eval"

D

Dirk Einecke

Hi.

I've a id and a value. Now I want a variable with id as name and the
content from value as value of this variable. I tryed like this:

id = 'foo'
value = 'This is the value.'
eval "$#{id} + ' = ' + #{value}" # -> $id = 'This is the value.'

But this is not working. What's wrong? Is there a better/faster way?

greetings
Dirk Einecke
 
O

Olivier D.

I've a id and a value. Now I want a variable with id as name and the
content from value as value of this variable. I tryed like this:

id = 'foo'
value = 'This is the value.'
eval "$#{id} + ' = ' + #{value}" # -> $id = 'This is the value.'

First, your eval string should be:
eval "#{id} = '#{value}'"
id and value are replaced to form the expression:
eval "foo = 'this is the value'"
which is OK with irb. But it doesn't work with ruby, and it's explained
here: http://tinyurl.com/2vc4u . One solution would be to use a hash to
store id, value as key, value...
 
K

Kaspar Schiess

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Dirk Einecke wrote:

| Hi.
|
| I've a id and a value. Now I want a variable with id as name and the
| content from value as value of this variable. I tryed like this:
|
| id = 'foo'
| value = 'This is the value.'
| eval "$#{id} + ' = ' + #{value}" # -> $id = 'This is the value.'
|
| But this is not working. What's wrong? Is there a better/faster way?
|
| greetings
| Dirk Einecke
|
|
Hello Dirk,

~ eval %Q{ $#{id} = #{value.inspect} }

will work for value being string values (and some others)..

And you will probably get to hear that this is insecure and has numerous
problems. Setting these variables as elements of a hash or open struct
is better design - of course your mileage may vary.

(If you are not told by others here, you might have a look at why PHP
does not do auto-creation of global variables anymore..)

kaspar

semantics & semiotics
code manufacture

www.tua.ch/ruby
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFA59PAFifl4CA0ImQRAphrAJ9483uwSTphc2i5O2rNDi8rZJUVjgCeIrbk
0YjXKZBjXX/akDS8+Pegg4g=
=QV4D
-----END PGP SIGNATURE-----
 
D

Dirk Einecke

Hi.

Kaspar said:
And you will probably get to hear that this is insecure and has numerous
problems. Setting these variables as elements of a hash or open struct
is better design - of course your mileage may vary.

Okay. It seems that a struct is a better way for me. I will try it on
this way.

greetings
Dirk Einecke
 
R

Richard Lionheart

Hi Kaspar,

I offered the code below to someone who wanted to output the values of a
hash as an array of arrays. I used 'eval' in order to display a sample
program statement and then execute the statement with cutting and pasting.

Then I ran across your response in the current thread and that led me to try
it out by appendind a couple of statement to produce additional output.
That led me to ask you the following:

1. 'inspect' produced correct output from the hash, but it's not formatted
at all. Is it correct that 'inspect' is really useful only for "quick and
dirty" dumping of an object's data and programming as done earlier in the
attached code is required for anything more?

2. Would you recommend any improvement to my coding in either of these
approaches?

Best wishes,
Richard
 
G

gabriele renzi

il Mon, 5 Jul 2004 19:18:46 -0400, "Richard Lionheart"
<[email protected]> ha scritto::


maybe you want the prettyprinter.rb module? it's in the standad
distribution.
 
R

Richard Lionheart

Thanks, Gabriele,

I found prettyprint.rb and looked at it for guidance on how to apply it. In
that effort I found a stale URL for a paper on pretty-printing by Philip
Wadler (which wouldn't be that helpful in actually *using* prettyprint.rb!).
A current reference to it is
http://homepages.inf.ed.ac.uk/wadler/topics/language-design.html#prettier.
It would be nice to get the updated URL introduced in the Ruby distribution,
but I don't know how to go about that.

If you have a suggestion, I'd follow up on it.

Regards,
Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,147
Messages
2,570,835
Members
47,382
Latest member
MichaleStr

Latest Threads

Top