Problem with "eval"

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

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

···

On 2004-07-04, Dirk Einecke <dirk.einecke@gmx.de> wrote:

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

--
Olivier D.

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

Hi.

Kaspar Schiess wrote:

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

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

···

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.713 / Virus Database: 469 - Release Date: 7/3/2004

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

···

il Mon, 5 Jul 2004 19:18:46 -0400, "Richard Lionheart" <NoOne@Nowhere.com> ha scritto::

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
Wadler: Language design.
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

"gabriele renzi" <surrender_it@remove.yahoo.it> wrote in message
news:e2pke0t2k1atj393svvv4t397bdi18o82j@4ax.com...

···

il Mon, 5 Jul 2004 19:18:46 -0400, "Richard Lionheart" > <NoOne@Nowhere.com> ha scritto::

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

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.713 / Virus Database: 469 - Release Date: 7/4/2004