[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

how to import variable from other .rb file?

camenix

10/28/2006 10:31:00 AM

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?

10 Answers

Jano Svitok

10/28/2006 10:47:00 AM

0

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

Jano Svitok

10/28/2006 10:49:00 AM

0

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

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

camenix

10/28/2006 11:05:00 AM

0

Thanks so much for you professinalism.

camenix

10/28/2006 11:30:00 AM

0

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

dblack

10/28/2006 12:02:00 PM

0

Robert Klemme

10/28/2006 12:03:00 PM

0

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

Kind regards

robert

Eero Saynatkari

10/28/2006 4:51:00 PM

0

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

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

10/28/2006 7:17:00 PM

0

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/proje...
http://codeforpeople.com/lib/ruby...

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...
http://rubyforge.org/pr...

HTH.

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

Joel VanderWerf

10/28/2006 7:38:00 PM

0

Joel VanderWerf wrote:
...
> http://raa.ruby-lang.org/proje...
> http://codeforpeople.com/lib/ruby...
>
> 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/...

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


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

camenix

10/29/2006 5:13:00 AM

0

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