It won’t work till use global varibale in
main.rb :
eval File.read(‘config.rb’)
puts $tool_mysql
OR:
main.rb :
tool_mysql=’’
eval File.read(‘config.rb’)
puts tool_mysql
What exactly does not work? Did you by chance test in IRB? That cannot
be
trusted with regard to local variables. Please show a bit more
(including
error message).
If by “help” you mean change the way eval works, then no Jan
already showed you some ways to read things in from another file, so
I’m not sure what you’re asking.
The first one (that’s the one I wrote) doesn’t let you grab local
variables from config.rb, but it does let you access constants
(including classes and modules) and methods defined in config.rb. The
constants and methods are wrapped up in a new module and you access them
through this module. This prevents namespace pollution.
There’s a simple example at:
/home/vjoel/ruby/prj/script/doc/index.html
and some more examples in the package’s example/ dir.
What exactly does not work? Did you by chance test in IRB? That cannot be
trusted with regard to local variables. Please show a bit more (including
error message).
Any variables defined in evalspace are not available outside evalspace.
eval ‘foo = 4’
p foo # Undefined error
p eval(‘foo’) # Fine
Definining the variable beforehand (as in the second workaround above)
will produce no problems.
The method load variable from other file list below:
Ruby didn’t has import method
|
|-use global variable
:Jan S.
|-use local variable
| |- eval File.read(‘config.rb’)
:Jan S.
| | --tool_mysql=’’ (add code beforehand)
:Robert K.
| - puts config = YAML.load_file(‘config.yaml’)
:Jan S.
| |- yaml’s syntax is diff from ruby
:
| - It is designed for this
=My choice
|-use constants
:Jan S.
-use script module
–if really want to use ruby syntax in config.rb
:Joel VanderWerf
–Add module import capabily ,It’s toomuch for me:-)
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.