how to import variable from other .rb file?

C

camenix

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

Jan Svitok

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']
 
J

Jan Svitok

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']

Except for the numbering ;-) Is seems I'm doing too many things at once...
 
C

camenix

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
 
D

dblack

Hi --

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.


David

--
David A. Black | (e-mail address removed)
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org
 
R

Robert Klemme

camenix said:
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).

Kind regards

robert
 
E

Eero Saynatkari

--pE9oldV/ca00Larp
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

=20
What exactly does not work? Did you by chance test in IRB? That cannot = be=20
trusted with regard to local variables. Please show a bit more (includin= g=20
error message).

Any variables defined in evalspace are not available outside evalspace.

eval 'foo =3D 4'
p foo # Undefined error
p eval('foo') # Fine

Definining the variable beforehand (as in the second workaround above)
will produce no problems.

--pE9oldV/ca00Larp
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (FreeBSD)

iD8DBQFFQ4nk7Nh7RM4TrhIRAnNCAKDXOB/obN1Soqc4hxd2S+Oq5EhNuQCdGRvv
98JSjboelLeycps8j96+gKg=
=Fcz5
-----END PGP SIGNATURE-----

--pE9oldV/ca00Larp--
 
J

Joel VanderWerf

camenix said:
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.
 
J

Joel VanderWerf

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
X:/mysql.exe
 
C

camenix

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,216
Messages
2,571,118
Members
47,720
Latest member
mohdkaif002

Latest Threads

Top