[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Global Array?

subsume@gmail.com

5/26/2007 3:12:00 AM

I've got a class which calls itself several times (recursive). While
this class is calling itself, I want it to be appending a global array (
<< ). I can't figure out how to make the array itself global, so for now
each time the class calls itself, it creates the array anew again. Help?

--
Posted via http://www.ruby-....

6 Answers

Trans

5/26/2007 3:24:00 AM

0



On May 25, 11:11 pm, Sy Ys <subs...@gmail.com> wrote:
> I've got a class which calls itself several times (recursive). While
> this class is calling itself, I want it to be appending a global array (
> << ). I can't figure out how to make the array itself global, so for now
> each time the class calls itself, it creates the array anew again. Help?

Have some code?

Try:

$a = []

T.


highlyjhi

5/26/2007 3:26:00 AM

0

On May 25, 2007, at 11:11 PM, Sy Ys wrote:

> I've got a class which calls itself several times (recursive). While
> this class is calling itself, I want it to be appending a global
> array (
> << ). I can't figure out how to make the array itself global, so
> for now
> each time the class calls itself, it creates the array anew again.
> Help?
>
> --
> Posted via http://www.ruby-....
>

Are you putting a dollar sign ($) in front of the variable's name?

ex: $global_array



Dan Zwell

5/26/2007 3:33:00 AM

0

Sy Ys wrote:
> I've got a class which calls itself several times (recursive). While
> this class is calling itself, I want it to be appending a global array (
> << ). I can't figure out how to make the array itself global, so for now
> each time the class calls itself, it creates the array anew again. Help?
>

class Klass
def recurse
$klasses = Array.new unless global_variables.include?("$klasses") and $klasses.is_a?(Array)
unless $klasses.size > 40
k = Klass.new
$klasses << k
k.recurse
end
end
end

Note that I found the "and $klasses.is_a?(Array)" bit necessary because
the parser seems to initialize the global array $klasses when it first
comes across the word. Might I suggest using a class level variable
instead (@@klasses)? It seems appropriate. They are "almost global."

Dan

Robert Klemme

5/26/2007 8:02:00 AM

0

On 26.05.2007 05:32, Dan Zwell wrote:
> Sy Ys wrote:
>> I've got a class which calls itself several times (recursive). While
>> this class is calling itself, I want it to be appending a global array (
>> << ). I can't figure out how to make the array itself global, so for now
>> each time the class calls itself, it creates the array anew again. Help?
>>
>
> class Klass
> def recurse
> $klasses = Array.new unless > global_variables.include?("$klasses") and $klasses.is_a?(Array)
> unless $klasses.size > 40
> k = Klass.new
> $klasses << k
> k.recurse
> end
> end
> end
>
> Note that I found the "and $klasses.is_a?(Array)" bit necessary because
> the parser seems to initialize the global array $klasses when it first
> comes across the word. Might I suggest using a class level variable
> instead (@@klasses)? It seems appropriate. They are "almost global."

No, rather use a class instance variable. But even that has problems
because it is not thread safe.

The solution really depends on the problem to solve. Maybe it's a
global variable, maybe it's a thread local, maybe it's a parameter like in

def foo(x=[])
if ...
# recursion
foo(x)
else
# termination
end
end

.... or maybe it's even another class with an instance variable, i.e.
factor out the recursive functionality into another class.

It all depends...

robert

subsume@gmail.com

5/26/2007 1:10:00 PM

0

Went with this. Thanks

Dan Zwell wrote:
> Might I suggest using a class level variable
> instead (@@klasses)? It seems appropriate. They are "almost global."


--
Posted via http://www.ruby-....

Paul Stickney

5/26/2007 5:52:00 PM

0

If it's a class calling itself, vs. an object (instance of the class)
calling itself, why not use instance variables of the class? Or ...
need more input on the exact problem.

"Global variables" and recursion sound like a bad idea in general
(could you not just pass the object through each recursive call?)