another newbie question

K

Kósa Márton

Hi group,

I apologize for the newbie question.

Is there in Ruby some possibility for making global
variables visible from within a DEF, except that
passing all of them as parameters?

E.g.:

class class1
....
def some_method
...
end
end

....
g = class1.new
....


def f(g) <--- i'd like to avoid to pass g here
....
g.some_method
....
end

I have not found any other solution in the documentation.
When the number of these global objects become large (e.g. >20),
passing all of them each time becomes incomfortable.
Any help appreciated.

Marton
 
T

Tim Hunter

Kósa Márton said:
Hi group,

I apologize for the newbie question.

Is there in Ruby some possibility for making global
variables visible from within a DEF, except that
passing all of them as parameters?

E.g.:

class class1
...
def some_method
...
end
end

...
g = class1.new
...


def f(g) <--- i'd like to avoid to pass g here
...
g.some_method
...
end

I have not found any other solution in the documentation.
When the number of these global objects become large (e.g. >20),
passing all of them each time becomes incomfortable.
Any help appreciated.

Marton

Variables with names that start with $ are global.

$g = class1.new

Bear in mind that using global variables is often considered bad form
because in large programs it is hard to figure out where the variable is
used and modified. If your program relies on a lot of global variables
you might consider re-thinking how your program works.

Whatever you decide to do, good luck!
 
K

Kósa Márton

Variables with names that start with $ are global.

$g = class1.new

Bear in mind that using global variables is often considered bad form
because in large programs it is hard to figure out where the variable is
used and modified. If your program relies on a lot of global variables
you might consider re-thinking how your program works.

Whatever you decide to do, good luck!

Thank you very much. The $ sign is my friend here.
I'll not misuse it.
 
D

David Morton

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


Thank you very much. The $ sign is my friend here.
I'll not misuse it.

Something else to consider, if the data is related, is to put it into =20=

a hash and pass that:

config =3D { :key =3D> 'value', :foo =3D> "bar", :someNumber =3D> "56", =20=

'someClass' =3D> class1.new}

def f(config)

config['someClass'].some_method

end



David Morton
Maia Mailguard http://www.maiamailguard.com
(e-mail address removed)



-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (Darwin)

iD8DBQFHeti/Uy30ODPkzl0RAn1TAJsEvlsOyqbYW1sA6qyJyszxXbN5TACfVzO9
E3SUegwXGrgTFMO+bnU8ja4=3D
=3DPam/
-----END PGP SIGNATURE-----
 
J

John Joyce

if your program grows to many files, and you still need $ globals,
you can simply create a file and name it something obvious like
"globals.rb" and do all of the initializing of the globals there.
For debugging or error checking you can simply keep track of which
ruby file was running when the last change was made to the global.

Also consider using instance variables which are prefixed with the @
sigil. These are visible within anything in that instance object of
the class. Rails is an example of heavy use of instance variables.
They can simplify life a lot, but sometimes it is better to pass data
to a function than to simply use the same instance variable
everywhere (such as when you want a function or method to use
encapsulation and do its own validation internally. When you have
something that interfaces with a lot of other things already, it can
be a good idea to keep the function/method's name and signature and
return the same but retain the ability to change its internals) .
 

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

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top