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
Kósa Márton wrote:
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!
···
--
RMagick: http://rmagick.rubyforge.org/
RMagick 2: http://rmagick.rubyforge.org/rmagick2.html
Thank you very much. The $ sign is my friend here.
I'll not misuse it.
···
On Jan 1, 10:53 pm, Tim Hunter <TimHun...@nc.rr.com> wrote:
Kósa Márton wrote:
> 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!
--
RMagick:http://rmagick.rubyforge.org/
RMagick 2:http://rmagick.rubyforge.org/rmagick2.html
Something else to consider, if the data is related, is to put it into a hash and pass that:
config = { :key => 'value', :foo => "bar", :someNumber => "56", 'someClass' => class1.new}
def f(config)
config['someClass'].some_method
end
David Morton
Maia Mailguard http://www.maiamailguard.com
mortonda@dgrmm.net
···
On Jan 1, 2008, at 5:19 PM, Kósa Márton wrote:
Thank you very much. The $ sign is my friend here.
I'll not misuse it.
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) .