Stumped newbie

Here is what I'm trying to do. Simulate nodes with objects. The nodes

are updated with the class's update method, now where I'm stuck is

acessing outside variables within the method.

Simply, how do I refer to an objects state within a method?

class Gate
  def initialize
    @state = rand(2)
  end
  def state
    @state
  end
  def update
    @state = a[1].state
  end
end

num = 2

a = Array.new
for b in 0..(num - 1)
  a[b] = Gate.new
  puts a[b].state
end

puts
puts a[0].state
a[0].update
puts a[0].state

Can anyone tell me how to do this?

I think you're asking how to make the "outside" variable (here, the
"a" Array) a global variable. You need to prefix its name with the "$"
character, i.e.

    class Gate
        def update
            @state = $a[1].state
        end
    end

and

    $a = Array.new
    num.times { |i|
        $a[i] = Gate.new
        etc.
    }

But of course, you really should avoid using global variables if you
can help it.

···

On Fri, 16 Jul 2004 03:12:16 +0900, flattree <esplair@yahoo.com> wrote:

Here is what I'm trying to do. Simulate nodes with objects. The nodes
are updated with the class's update method, now where I'm stuck is
accessing outside variables within the method.

is this what you mean?

   class Gate
     def update other
       @state = other.state
     end
   end

   a[0].update a[1]
   puts a[0].state

i'm not totally clear what 'a' is supposed to be in your original update
methods.

cheers.

-a

···

On Thu, 15 Jul 2004, flattree wrote:

Here is what I'm trying to do. Simulate nodes with objects. The nodes

are updated with the class's update method, now where I'm stuck is

acessing outside variables within the method.

Simply, how do I refer to an objects state within a method?

class Gate
def initialize
   @state = rand(2)
end
def state
   @state
end
def update
   @state = a[1].state
end
end

num = 2

a = Array.new
for b in 0..(num - 1)
a[b] = Gate.new
puts a[b].state
end

puts
puts a[0].state
a[0].update
puts a[0].state

Can anyone tell me how to do this?

--

EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
A flower falls, even though we love it;
and a weed grows, even though we do not love it. --Dogen

===============================================================================

You could try something like this:

---begin---
class Gate
  @@gates = Array.new

  def Gate.[](key)
    @@gates[key]
  end

  def Gate.<<(g)
    @@gates << g
  end

  def Gate.each
    @@gates.each { yield }
  end

  def initialize
    @state = rand(2)
    @@gates << self
  end

  def state
    @state
  end

  def update
    @state = @@gates.first.state
    self
  end
end

g = Gate.new
=> #<Gate:0x27f2218 @state=1>
h = Gate.new
=> #<Gate:0x27dbb08 @state=0>

g.state
=> 1
h.state
=> 0

Gate[0] == g
=> true
Gate[1] == h
=> true

h.update
=> #<Gate:0x27dbb08 @state=0>
h.state
=> 1
---end---

Does that fit what you're looking for?

Lennon

I think you're asking how to make the "outside" variable (here, the
"a" Array) a global variable. You need to prefix its name with the "$"
character, i.e.

    class Gate
        def update
            @state = $a[1].state
        end
    end

and

    $a = Array.new
    num.times { |i|
        $a[i] = Gate.new
        etc.
    }

But of course, you really should avoid using global variables if you
can help it.

Yes, that is very much what I'm looking for. At this stage it works.
Thank you.