2012-08-22 5 views
1

이전 질문 (Is a method in ruby similar to a subroutine?)에서 Ruby의 메소드에 대해 질문했습니다. 이제, 필자의 첫 번째 방법을 쓰면서 변수의 범위에 문제가 생겼다. 아래의 프로그램은 learn 메서드를 호출하지 않을 때 해석하고 잘 실행됩니다. 즉, 33 행에서 learn(2) 전화를 제거하면 모든 것이 올바르게 작동하며 주 프로그램과 메소드에서 다양한 변수 (예 : stimulus[])를 사용하는 것이 중요하지 않습니다. 그러나 전화를 걸었을 때 (u 키를 눌러서 사용하는 경우), 아래 메시지가 나타납니다.이 방법에서는 stimulus을 사용하는 것이 적절하지 않음을 나타냅니다.메소드의 변수 문제

brain.rb:26:in `block in learn': undefined local variable or method `stimulus' for main:Object (NameError) 
    from brain.rb:25:in `each' 
    from brain.rb:25:in `learn' 
    from brain.rb:33:in `ucr' 
    from brain.rb:69:in `<main>' 

는하지만 메인 프로그램에 의해 결정이, 자신의 현재 값으로 (뇌)를 사용합니다. 내가 겪은 범위에 대한 질문에 대한 모든 대답은 다른 방식, 즉 다른 곳의 메소드에서 변수를 사용하는 문제를 해결하는 것처럼 보입니다. 나는 자극과 두뇌를 세계화하는 것을 생각했지만 분명히 그것은 아니오입니다. 프로그램에서 변수를 사용하는 방법을 어떻게 알 수 있습니까?

ps. 이 방법이 작동하면 프로그램의 여섯 곳에서 전화 할 것입니다.

require 'matrix' 
class Matrix 
    def []=(i, j, x) 
    @rows[i][j] = x 
    end 
end #code to allow putting individual elements in matrix at i,j 
def read1maybe 
    return $stdin.read_nonblock 1 
rescue Errno::EAGAIN 
    return '' 
end # part of code to get keypress 
brain= Matrix[ [0,0,0,0,99,0,0,0,0,1,0], 
       [0,0,0,0,0,99,0,0,0,1,0], 
       [0,0,0,0,0,0,99,0,0,1,0], 
       [25,0,0,0,0,0,0,1,-1,1,-99], 
       [0,23,0,0,0,0,0,1,-1,1,1], 
       [0,0,24,0,0,0,0,1,-1,1,1], 
       [0,0,0,22,0,0,0,1,-1,1,1] ] 
stimulus=Matrix.column_vector([0,0,0,0,0,0,0,0,0,0,0]) 
behavior=Matrix.column_vector([0,0,0,0,0,0,0]) 
t=500 # t=threshold 
energy=50 
# begin defining behavioral methods 
def learn(ix) 
    for j in (7..10) 
    if stimulus[j]>0 && brain[ix,j] != 0 && brain[ix,j] < 99 then 
     brain[ix,j]+=int(0.1 * stimulus[j]) * (99-brain[ix,j]) 
    end # if stim 
    end # for j 
end # learn 
def ucr 
    puts "Show UCR" 
    learn(2) 
end 
def positive_fixer 
    puts "Positive fixer" 
end 
def negative_fixer 
    puts "Negative fixer" 
end 
# end defining behavioral methods 

# begin main program 
while(energy>0) do 
(0..10).each {|n| if stimulus[n,0]>2 then stimulus[n,0]+= -2 else stimulus[n,0]==0 end} 
input=false 
system 'stty cbreak' 
look=0 
while look < 40000 
    q = read1maybe 
    break if q.length > 0 
    look +=1 
end # while look 
case q 
when "f" then stimulus[4,0]=9 and puts "Good!" 
when "p" then stimulus[5,0]=9 and puts "Bad!" 
when "u" then stimulus[6,0]=9 
when "l" then stimulus[7,0]=9 and stimulus[8,0]=9 and puts "ight on" 
when "c" then stimulus[9,0]=9 and puts " BUZZZ" 
input=true 
end # case q 
system 'stty cooked' 

if input==false then (0..3).each { |n| stimulus[n,0]=rand(25)} end 

behavior=brain*stimulus 
if behavior[0,0] > t then positive_fixer end 
if behavior[1,0] > t then negative_fixer end 
if behavior[2,0] > t then ucr end 
if behavior [3,0] > t then puts "show operant 1" end # and stimulus[10,0]=9 
if behavior[4,0] > t then puts "show operant 2" end 
if behavior[5,0] > t then puts "show operant 3" end 
if behavior[6,0] > t then puts "show operant 4" end 
energy += -1 
# temp to test development of memory 
puts brain[2,9] 
end # while energy > 0 
puts 
puts "It's dead Jim." 
# end main program 

답변

1

stimulusbrain 및 방법은 외부에서 선언된다. 당신과 같이 매개 변수로에 전달해야

def learn(ix, brain, stimulus) 
    for j in (7..10) 
    if stimulus[j]>0 && brain[ix,j] != 0 && brain[ix,j] < 99 then 
     brain[ix,j]+=int(0.1 * stimulus[j]) * (99-brain[ix,j]) 
    end # if stim 
end # for j end # l 

그리고 같은 ucr 편집 :

def ucr(brain, stimulus) 
    puts "Show UCR" 
    learn(2, brain, stimulus) 
end 

을 그리고 ucr(brain, stimulus) 같은 ucr를 호출합니다. 패턴을 봐? 매개 변수를 사용하는 메서드 정의에 매개 변수를 추가 한 다음 메서드를 호출 할 때 매개 변수를 전달해야합니다.

+0

그런가요? 그것들이 행렬이라는 것이 중요합니까? – user918069

+0

예, 객체를 전달하는 중 –

+0

두뇌 및 자극 매트릭스를 매개 변수로 전달하는 방법은 무엇입니까? 시도해봤을 때 brain.rb : 24 :'배울 ': 잘못된 인수 (ArgumentError) (ArgumentError) \t brain.rb : 33 : ucr의 \t brain.rb : 69 : '

' – user918069

관련 문제