G
Geert Fannes
------_=_NextPart_001_01C56514.8445A09C
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
Hello, what is the scope of a variable created inside an eval()
statement? To demonstrate what I mean, I created the following program:
=20
a=3D1
puts local_variables.sort.collect{|v|"1 #{v}: #{eval(v)}"}
eval("b=3D2")
puts local_variables.sort.collect{|v|"2 #{v}: #{eval(v)}"}
c=3Deval("b")
d=3Db
puts local_variables.sort.collect{|v|"3 #{v}: #{eval(v)}"}
=20
This gives as output:
=20
1 a: 1
1 c:
1 d:
2 a: 1
2 b: 2
2 c:
2 d:
tmp.rb:6: undefined local variable or method `b' for main:Object
(NameError)
=20
Apparently, Ruby creates immediately the variables a,c,d (so all the
variables that are on the left-hand-side of an assignment), even before
he actually processed the assignment.
After processing the line "eval("b=3D2")", Ruby also created the local
variable b, which is accessible via eval("b"), but not directly (Ruby
crashes on line 6, "d=3Db" because he does not know local variable b,
which is clearly NOT the case, since he did write out local variable b
correctly).
=20
Any idea how these things work in Ruby and why my above program does
not?
=20
If you are wondering why I need this: I want to develop a
"named-argument" system for ruby-methods. My plan is to pass in a hash
all the optional arguments and automatically create variables for all
the keys in this hash. This generic procedure to create variables from
the hash-keys, does not know the names for these variables, but the rest
of the method that uses the named arguments does:
=20
def
some_method(required_arg1,required_arg2,opt_arg_hash=3D{pt_arg1=3D>nil,=
p
t_arg2=3D>nil})
#this generic method should create the variables opt_arg1 and opt_arg2
process_opt_args(opt_arg_hash)
=20
#here we should be able to use the variables opt_arg1 and opt_arg2
directly, without having
#to write opt_arg_hash[pt_arg1] and opt_arg_hash[pt_arg2]
puts(opt_arg1)
puts(opt_arg2)
end
=20
Greetings,
Geert.
------_=_NextPart_001_01C56514.8445A09C--
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
Hello, what is the scope of a variable created inside an eval()
statement? To demonstrate what I mean, I created the following program:
=20
a=3D1
puts local_variables.sort.collect{|v|"1 #{v}: #{eval(v)}"}
eval("b=3D2")
puts local_variables.sort.collect{|v|"2 #{v}: #{eval(v)}"}
c=3Deval("b")
d=3Db
puts local_variables.sort.collect{|v|"3 #{v}: #{eval(v)}"}
=20
This gives as output:
=20
1 a: 1
1 c:
1 d:
2 a: 1
2 b: 2
2 c:
2 d:
tmp.rb:6: undefined local variable or method `b' for main:Object
(NameError)
=20
Apparently, Ruby creates immediately the variables a,c,d (so all the
variables that are on the left-hand-side of an assignment), even before
he actually processed the assignment.
After processing the line "eval("b=3D2")", Ruby also created the local
variable b, which is accessible via eval("b"), but not directly (Ruby
crashes on line 6, "d=3Db" because he does not know local variable b,
which is clearly NOT the case, since he did write out local variable b
correctly).
=20
Any idea how these things work in Ruby and why my above program does
not?
=20
If you are wondering why I need this: I want to develop a
"named-argument" system for ruby-methods. My plan is to pass in a hash
all the optional arguments and automatically create variables for all
the keys in this hash. This generic procedure to create variables from
the hash-keys, does not know the names for these variables, but the rest
of the method that uses the named arguments does:
=20
def
some_method(required_arg1,required_arg2,opt_arg_hash=3D{pt_arg1=3D>nil,=
p
t_arg2=3D>nil})
#this generic method should create the variables opt_arg1 and opt_arg2
process_opt_args(opt_arg_hash)
=20
#here we should be able to use the variables opt_arg1 and opt_arg2
directly, without having
#to write opt_arg_hash[pt_arg1] and opt_arg_hash[pt_arg2]
puts(opt_arg1)
puts(opt_arg2)
end
=20
Greetings,
Geert.
------_=_NextPart_001_01C56514.8445A09C--