How to import variable from other .rb file?

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?

On 10/28/06, camenix [email protected] wrote:

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?

eval File.read(‘config.rb’)

  1. use constants:
    TOOL_MYSQL = ‘dsfsdffds’

puts TOOL_MYSQL

  1. use global variables:
    config.rb :
    $tool_mysql = ‘cxxx’

puts $tool_mysql

  1. use YAML for configuration:
    config.yaml:
    tool_mysql: cxdsfsdf

require ‘yaml’
config = YAML.load_file(‘config.yaml’)
puts config[‘tool_mysql’]

Thanks so much for you professinalism.

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

On 10/28/06, Jan S. [email protected] wrote:

TOOL_MYSQL = ‘dsfsdffds’
config.yaml:
tool_mysql: cxdsfsdf

require ‘yaml’
config = YAML.load_file(‘config.yaml’)
puts config[‘tool_mysql’]

Except for the numbering :wink: Is seems I’m doing too many things at
once…

camenix [email protected] wrote:

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).

Kind regards

robert

Hi –

On Sat, 28 Oct 2006, camenix wrote:

eval File.read(‘config.rb’)
puts tool_mysql

If by “help” you mean change the way eval works, then no :slight_smile: Jan
already showed you some ways to read things in from another file, so
I’m not sure what you’re asking.

David

camenix wrote:

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?

Using $globals or CONSTANTS is fine for simple programs, but these
pollute the global namespace.

If you really want to use ruby syntax in config.rb (rather than YAML or
other text formats), there are some alternatives:

http://raa.ruby-lang.org/project/script/
http://codeforpeople.com/lib/ruby/dynaload/

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.

You might also find these projects interesting:

http://raa.ruby-lang.org/project/amarshal/
http://rubyforge.org/projects/ron

HTH.

On 2006.10.28 21:05, Robert K. wrote:

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).

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.

Joel VanderWerf wrote:

/home/vjoel/ruby/prj/script/doc/index.html

Oops, I was browsing the local docs blush . Use this link instead:

http://redshift.sourceforge.net/script/doc/index.html

Here’s one way to handle your config.rb file:

$ cat config.rb
def tool_mysql; ‘X:/mysql.exe’; end
TOOL_MYSQL = ‘X:/mysql.exe’

$ cat main.rb
require ‘script’

config = Script.load “config.rb”

puts config.tool_mysql
puts config::TOOL_MYSQL

$ ruby main.rb
X:/mysql.exe
X:/mysql.exe

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:-)