=========================
cat list
=========================
lic_servers = {
‘[email protected] ’ => 10,
‘[email protected] ’ => 10
}
=========================
cat test.rb
=========================
#!/usr/bin/env ruby
csh_file = ‘list’
sign = cat #{csh_file}
a = eval(sign)
puts a
puts lic_servers
=========================
3. run test.rb
{“[email protected] ”=>10, “[email protected] ”=>10}
test.rb:8:in <main>': undefined local variable or method
lic_servers’
for main:Object (NameError)
Why it report ‘lic_servers’ undefined? I hope lic_servers set by ‘eval’,
but seems doesn’t work, why?
The object set lic_servers in file ‘list’ is for the servers list
always change, I want split it from main ruby script.
previn
January 9, 2014, 10:41am
#2
See
http://stackoverflow.com/questions/17172187/variable-assignment-using-eval
Eval creates the variable in his own scope. If you want is to be
available you should use an instance variable
@lic_servers = {…}
and in test.rb
puts @lic_servers
Lars
previn
January 9, 2014, 11:21am
#3
Another way is to define the local var before the eval so that it will
be accessible after it.
=========================
2. cat test.rb
#!/usr/bin/env ruby
lic_servers = Hash.new # <-- Define it before
csh_file = ‘list’
sign = cat #{csh_file}
a = eval(sign)
puts a
puts lic_servers
=====
Another example.
10.times do |x|
my_local = x
end
if defined? my_local
puts “my_local = #{my_local}”
else
puts “my_local is not defined”
end
Second round with local var being defined outside the block
my_local = 0
10.times do |x|
my_local = x
end
if defined? my_local
puts “my_local = #{my_local}”
else
puts “my_local is not defined”
end
This outputs
my_local is not defined
my_local = 9
Abinoam Jr.
previn
January 10, 2014, 2:37am
#4
Lars Vonk wrote in post #1132612:
See
http://stackoverflow.com/questions/17172187/variable-assignment-using-eval
Eval creates the variable in his own scope. If you want is to be
available you should use an instance variable
@lic_servers = {…}
and in test.rb
puts @lic_servers
Lars
Eval creates the variable in his own scope
Understand now, many thanks.
If you want is to be
available you should use an instance variable
@lic_servers = {…}
May I ask one more question? As I know, instance variable should belongs
to specific class, so here @lic_servers belongs to which class, Main
class?
previn
January 14, 2014, 9:34am
#5
Dear Alex,
Thank you so much for your guide and help, I understand now.
previn
January 10, 2014, 3:40am
#6
On Thu, Jan 9, 2014 at 5:37 PM, Previn L. [email protected] wrote:
May I ask one more question? As I know, instance variable should belongs
to specific class, so here @lic_servers belongs to which class, Main
class?
This is a great question. Actually, an instance variable belongs to an
instance , not a class. You can tell which instance by looking at the
self variable. At the top level self points to a magic object called
main (not Main) so that’s where @lic_servers lives.
Type “self” and “self.class” at irb to see for yourself.
If it’s still unclear, maybe my slides at
http://codelikethis.com/lessons/ruby_objects/objects#state will
clarify.