Forum: Ruby Global Array?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3151cc3624fa4f131c4a33ef4d67531d?d=identicon&s=25 Sy Ys (yeago)
on 2007-05-26 05:11
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?
45196398e9685000d195ec626d477f0e?d=identicon&s=25 Trans (Guest)
on 2007-05-26 05:24
(Received via mailing list)
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.
Fb971283c36f4e1738f21ec42bc087a7?d=identicon&s=25 highlyjhi (Guest)
on 2007-05-26 05:27
(Received via mailing list)
On May 25, 2007, at 11:11 PM, Sy Ys wrote:

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

ex: $global_array
Caf38c89d40443a858741b61ac6d82de?d=identicon&s=25 Dan Zwell (Guest)
on 2007-05-26 05:33
(Received via mailing list)
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
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2007-05-26 10:05
(Received via mailing list)
On 26.05.2007 05:32, Dan Zwell wrote:
>       global_variables.include?("$klasses") and $klasses.is_a?(Array)
> 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
3151cc3624fa4f131c4a33ef4d67531d?d=identicon&s=25 Sy Ys (yeago)
on 2007-05-26 15:09
Went with this. Thanks

Dan Zwell wrote:
> Might I suggest using a class level variable
> instead (@@klasses)? It seems appropriate. They are "almost global."
C2253345d045285df6751f7d65ff8569?d=identicon&s=25 Paul Stickney (Guest)
on 2007-05-26 19:52
(Received via mailing list)
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?)
This topic is locked and can not be replied to.