Global variables usage

D

di vi

I declared two files.One to hold the value of a global variable.And the
other to print it.


1)to hold variables (var.rb)

Code:

$foo = 500


2) to print the global variable (gprint.rb)

Code:
loop do
puts $foo
end



But when I print it ,I get the value "nil" being printed not 500.What
could be the problem?

I did read some tutorials online but dint help.Could some please tell
why I am not able to access the global variable between two .rb files in
the same folder?

Please let me know.
 
B

botp

I declared two files.One to hold the value of a global variable.And the
other to print it.


1)to hold variables (var.rb)

Code:

=A0 =A0$foo =3D 500


2) to print the global variable (gprint.rb)

Code:
=A0 =A0loop do
=A0 =A0puts $foo
=A0 =A0end

where in this code (gprint.rb) does it say that you are accessing the
file var.rb ?
read on require and load...

best regards -botp
 
D

di vi

botp said:
where in this code (gprint.rb) does it say that you are accessing the
file var.rb ?
read on require and load...

best regards -botp

Load and require execute the complete program..I want only the value of
global variables to change in the respective .rb file.
 
R

Robert Klemme

2010/2/19 di vi said:
Load and require execute the complete program..I want only the value of
global variables to change in the respective .rb file.

Well, there's an obvious solution to that: place the global variable
in a file on its own which you load from all places that need it.
However, I am not sure that this is generally a good idea. What do
you need that for?

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 

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
473,772
Messages
2,569,593
Members
45,108
Latest member
AlbertEste
Top