Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8418901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:27:24+00:00 2026-06-10T02:27:24+00:00

In a previous question ( Is a method in ruby similar to a subroutine?

  • 0

In a previous question ( Is a method in ruby similar to a subroutine?) I asked about methods in Ruby. Now, writing my first ever method, I’ve clearly run into trouble with the scope of variables. The program below interprets and runs fine when I don’t call the method learn. That is, if I remove the call learn(2) in line 33, everything works fine and it doesn’t seem to matter that I use various variables (e.g. stimulus[]) both in the main program and in the method. But when I insert the call (and use it by pushing the u key), I get the message below, apparently indicating it’s not alright to use stimulus in the method.

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>'

But I NEED to use it (and brain) there, and with their present values as determined by the main program. All the answers to questions about scope that I’ve come across seem to go the other way, i.e, problems using variables in a method elsewhere. I thought of making stimulus and brain global, but apparently that is a no-no. How do I tell the method to use variables from the program?

Ps. Once this method works, I will be calling it from six other places in the program.

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T02:27:26+00:00Added an answer on June 10, 2026 at 2:27 am

    stimulus and brain are declared outside of the method. You need to pass them in as parameters like so:

    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
    

    And edit ucr like so:

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

    And invoke ucr like ucr(brain, stimulus). See the pattern? You need to add the parameters to the method definitions that use them and then pass them in when invoking the method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I asked a previous question about serialization and validation. Someone mentioned using the JSON
In a previous question about overriding Time.now , I was working toward a solution
So this is a follow-up to a previous question that I asked: Trying to
In a previous question: Getting "This method or property cannot be called on Null
Following on from my previous question about tables I am trying to create additional
This question is related to my previous question Link The method bellow does excatly
Follow on from a previous question One first screen activity I am clicking a
This is similar to my previous question, but that solution did not solve this
Following on from a previous question about sub-selects, I have an SQL statement with
In a previous question , I asked how to tell my Gemfile whether to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.