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?

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

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

···

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?

camenix 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?

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.

···

--
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Except for the numbering :wink: 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

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

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

Joel VanderWerf wrote:
...

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

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

···

--
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Thanks so much for you professinalism.

Hi --

···

On Sat, 28 Oct 2006, camenix 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

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

--
                   David A. Black | dblack@wobblini.net
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] Ruby for Rails | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org

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

The method load variable from other file list below:

Ruby didn't has import method

-use global variable

       :Jan Svitok

-use local variable
         >- eval File.read('config.rb')

        :Jan Svitok

         > \--tool_mysql='' (add code beforehand)

:Robert Klemme

         \- puts config = YAML.load_file('config.yaml')

:Jan Svitok

                           >- yaml's syntax is diff from ruby

    :

                           \- It is designed for this

         =My choice

-use constants

        :Jan Svitok
\-use script module
      \--if really want to use ruby syntax in config.rb
:Joel VanderWerf
                \--Add module import capabily ,It's toomuch for me:-)

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