Stumped newbie...

F

flattree

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 = Gate.new
puts a.state
end

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


Can anyone tell me how to do this?
 
L

Lyle Johnson

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.

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 = Gate.new
etc.
}

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

Ara.T.Howard

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 = Gate.new
puts a.state
end

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


Can anyone tell me how to do this?


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
--
===============================================================================
| 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
===============================================================================
 
L

Lennon Day-Reynolds

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
 
F

flattree

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 = 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.
 

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

Similar Threads

stumped 4
Chatbot 0
Problem with ruby code. 3
simple newbie question 2
[ANN] state-0.4.2 0
Need for speed -> a C extension? 27
Newbie 0
Is it possible to break a loop from within an internal lambda function? 7

Members online

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top