The correct thing is to write your method so it accepts parameters:
def current_trace(c, r, q)
puts "Counter: #{ c }; r: #{ r }; q: #{ q }"
end
Then call it like:
d = 1
r = 5
q = 0
counter = 0
while r >= d
current_trace(counter, r, q)
r = r - d
q = q + 1
counter += 1
end
current_trace(counter, r, q)
Which results in:
Counter: 0; r: 5; q: 0
Counter: 1; r: 4; q: 1
Counter: 2; r: 3; q: 2
Counter: 3; r: 2; q: 3
Counter: 4; r: 1; q: 4
Counter: 5; r: 0; q: 5
(I tweaked your variable names because your code won't work because you don't show where a
and d
come from, but that's beside the point.)
You can use @instance
, $global
or CONSTANTs
but those are playing games with variable scoping, which can cause problems when you accidentally change them in some method, often because you mistype them or leave off the sigil (@
, $
) and inadvertently create a local variable that doesn't change the one you want. Having to use @var
, or $var
at the main level, just to get your code to work, is a pretty good sign you're doing something wrong actually.