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.
Except for the numbering Is seems I'm doing too many things at once...
···
On 10/28/06, Jan Svitok <jan.svitok@gmail.com> wrote:
On 10/28/06, camenix <vipeak@gmail.com> wrote:
> Hello all,
> I have two file :
>
> config.rb:
> tool_mysql='X:/mysql.exe'
>
> main.rb :
> require "config.rb"
> puts tool_mysql
>
> It didn't work.for main.rb can not import variable from config.rb
>
> Is there any mothod to do this without parsing config.rb?
>
> Is here anyone who can help me?
1.
eval File.read('config.rb')
3. use constants:
TOOL_MYSQL = 'dsfsdffds'
puts TOOL_MYSQL
2. use global variables:
config.rb :
$tool_mysql = 'cxxx'
puts $tool_mysql
3. use YAML for configuration:
config.yaml:
tool_mysql: cxdsfsdf
Is here anyone who can help me?
1. eval File.read('config.rb')
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
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
Oops, I was browsing the local docs *blush* . Use this link instead:
Is here anyone who can help me?
1. eval File.read('config.rb')
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
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.
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).
Kind regards
robert
···
camenix <vipeak@gmail.com> wrote:
Is here anyone who can help me?
1. eval File.read('config.rb')
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
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.
···
On 2006.10.28 21:05, Robert Klemme wrote:
camenix <vipeak@gmail.com> wrote:
>Is here anyone who can help me?
>1. eval File.read('config.rb')
>
>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).