Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 方法中变量的问题_Ruby_Variables_Scope - Fatal编程技术网

Ruby 方法中变量的问题

Ruby 方法中变量的问题,ruby,variables,scope,Ruby,Variables,Scope,在前面的一个问题()中,我询问了Ruby中的方法。现在,在编写我的第一个方法时,我显然遇到了变量范围的问题。当我不调用方法learn时,下面的程序解释并运行良好。也就是说,如果我删除第33行中的调用learn(2),一切都会正常工作,我在主程序和方法中使用各种变量(例如stimulation[])似乎并不重要。但是当我插入调用(并通过按下u键来使用它)时,我得到下面的消息,显然表明在方法中使用stimultium是不正确的 brain.rb:26:in `block in learn': und

在前面的一个问题()中,我询问了Ruby中的方法。现在,在编写我的第一个方法时,我显然遇到了变量范围的问题。当我不调用方法
learn
时,下面的程序解释并运行良好。也就是说,如果我删除第33行中的调用
learn(2)
,一切都会正常工作,我在主程序和方法中使用各种变量(例如
stimulation[]
)似乎并不重要。但是当我插入调用(并通过按下
u
键来使用它)时,我得到下面的消息,显然表明在方法中使用
stimultium
是不正确的

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>'
brain.rb:26:in'block in learn':未定义的局部变量或main:Object的方法'stimulation'(NameError)
来自大脑。rb:25:in'each'
来自大脑。rb:25:in‘learn’
来自大脑。rb:33:in'ucr'
来自大脑。rb:69:in`'
但是我需要在那里使用它(和大脑),并且它们的当前值由主程序决定。我遇到的所有关于范围的问题的答案似乎都是相反的,即在其他地方的方法中使用变量的问题。我曾想过让刺激和大脑全球化,但显然这是一个禁忌。我如何告诉使用程序变量的方法

一旦这个方法起作用,我将从程序中的其他六个地方调用它

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
需要“矩阵”
类矩阵
def[]=(i,j,x)
@行[i][j]=x
结束
结束#代码,允许将单个元素放入i,j处的矩阵中
def read1maybe
返回块1上的$stdin.read\u
拯救厄尔诺:伊根
返回“”
结束#获取按键的部分代码
大脑=矩阵[[0,0,0,0,99,0,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] ]
刺激=矩阵。列向量([0,0,0,0,0,0,0,0,0,0])
行为=矩阵.列向量([0,0,0,0,0,0,0])
t=500#t=阈值
能量=50
#开始定义行为方法
def读入(ix)
对于j in(7..10)
如果刺激[j]>0&大脑[ix,j]!=0&&brain[ix,j]<99
大脑[ix,j]+=int(0.1*刺激[j])*(99大脑[ix,j])
结束#如果刺激
结束#对于j
结束#学习
def ucr
将“显示UCR”
学习(2)
结束
def正极固定器
“积极修正者”
结束
def负片定影器
“负定影器”
结束
#终端定义行为方法
#开始主程序
当(能量>0)时
(0..10)
输入=假
系统“stty cbreak”
外观=0
而看<40000
q=Read1可能是
如果q.length>0,则中断
外观+=1
边看边结束
案例q
当“f”时,则刺激[4,0]=9,并放入“Good!”
当“p”时,刺激[5,0]=9,并放入“Bad!”
当“u”时,则刺激[6,0]=9
当“l”时,刺激[7,0]=9,刺激[8,0]=9,并打开“灯”
当“c”时,刺激[9,0]=9,并将“BUZZZ”
输入=真
结束#案例q
系统“stty-cooked”
如果输入=false,则为(0..3)。每个{n |刺激[n,0]=rand(25)}结束
行为=大脑*刺激
如果行为[0,0]>t,则正固定器结束
如果行为[1,0]>t,则负固定器结束
如果行为[2,0]>t,则ucr结束
如果行为[3,0]>t,则将“显示操作1”放在末尾,刺激[10,0]=9
如果行为[4,0]>t则结束“show operant 2”
如果行为[5,0]>t则结束“show operant 3”
如果行为[6,0]>t则结束“show operant 4”
能量+=-1
#测试内存开发的temp
大脑[2,9]
当能量>0时结束#
放
写着“死了,吉姆。”
#结束主程序

刺激
大脑
在方法之外声明。您需要将它们作为参数传入,如下所示:

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
ucr(大脑,刺激)
。看到模式了吗?您需要将参数添加到使用它们的方法定义中,然后在调用该方法时将它们传入。

stimulus
brain
在方法之外声明。您需要将它们作为参数传入,如下所示:

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
ucr(大脑,刺激)
。看到模式了吗?您需要将参数添加到使用它们的方法定义中,然后在调用该方法时传入它们。

就像这样吗?它们是矩阵有关系吗?是的,你在传递objectsHow,那么我传递大脑和刺激矩阵作为参数吗?当我尝试时,我得到了大脑。rb:24:in
learn:来自大脑的参数数量错误(1对3)(ArgumentError)。rb:33:in
ucr'来自大脑。rb:69:in `'传递它们就像传递第九个参数的2一样。我会更新答案。托德,谢谢!然而,刺激是一个有10个值的列矩阵。大脑是一个7x11的矩阵。我试着学习(2,刺激,大脑)(和其他变化),但都返回未定义的局部变量。就像那样?它们是矩阵有关系吗?是的,你在传递objectsHow,那么我传递大脑和刺激矩阵作为参数吗?当我尝试时,我得到了大脑。rb:24:in
learn:来自大脑的参数数量错误(1对3)(ArgumentError)。rb:33:in
ucr'来自大脑。rb:69:in `'传递它们就像传递第九个参数的2一样。我会更新答案。托德,谢谢!然而,刺激是一个有10个值的列矩阵。大脑是一个7x11的矩阵。我试着学习(2,刺激,大脑)(和其他变化),但都返回未定义的局部变量。