Problem with eval

sorry but your syntax is wrong. Also.. value is a string, so when you eval
after my fix it does this.. $foo = this is a value . that is not right.
you need to put quotes around it like the example below.

id = 'foo'
value = 'This is the value.'
eval "$#{id} = \"#{value}\""
# $foo => "This is a value"
----David Ross

···

----------------
dross [at] code (-dash-) exec %dot% net

#############################33

irb(main):003:0> eval "$#{id} + ' = ' + #{value}" # -> $id = 'This is the
value.'
SyntaxError: (eval):1:in `irb_binding': compile error
(eval):1: syntax error
$foo + ' = ' + This is the value.
                      ^
(eval):1: syntax error
$foo + ' = ' + This is the value.
                                 ^
        from (irb):3
        from (irb):3
irb(main):004:0> eval "$#{id} + ' = ' + #{value}"
SyntaxError: (eval):1:in `irb_binding': compile error
(eval):1: syntax error
$foo + ' = ' + This is the value.
                      ^
(eval):1: syntax error
$foo + ' = ' + This is the value.
                                 ^
        from (irb):4
        from (irb):4
irb(main):005:0> eval "$#{id} = #{value}"
SyntaxError: (eval):1:in `irb_binding': compile error
(eval):1: syntax error
$foo = This is the value.
                         ^
        from (irb):5
        from (irb):5
irb(main):006:0> eval "$#{id} = \"#{value}\""
=> "This is the value."
irb(main):007:0> $foo
=> "This is the value."

Original Message:
-----------------
From: Dirk Einecke dirk.einecke@gmx.de
Date: Sun, 4 Jul 2004 16:57:44 +0900
To: ruby-talk@ruby-lang.org (ruby-talk ML)
Subject: 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

--------------------------------------------------------------------
mail2web - Check your email from the web at
http://mail2web.com/ .