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 6354077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:31:07+00:00 2026-05-24T22:31:07+00:00

When I run my code I get the following: cities.rb:70:in `sell_is_valid’: undefined method `[]’

  • 0

When I run my code I get the following:

cities.rb:70:in `sell_is_valid': undefined method `[]' for nil:NilClass (NoMethodError)
    from cities.rb:103
    from cities.rb:79:in `each'
    from cities.rb:79

Here is the code for that:

def sell_is_valid(num, type)
    puts "The most loads of #{type} you can sell is #{@train[num]}." #Line 66
    puts "How many #{type} would you like to sell?"
    prompt; value = gets.chomp.to_i
    if @train[num] <= value
        @wallet = @wallet + (value * @cities[@current_location][num]) #Line 70
        @storage = @storage + value
        @train[num] = @train[num] - value
    else
        puts "You're trying to buy too much!"
    end
end

So then what I did was did a puts on line 70.

puts @wallet
puts @cities[@current_location][num]

Same error this time on the puts @cities[@current_location][num] line. So I know that is what is making it do this. However, I use that line all through my code. For example, do the to the flow of my game (you have to buy something before you can sell it) that exact same line is executed three times in the buy_is_valid function with no issues. I also did a puts on num and it is giving me a valid number. It also is being used correctly in line 66.

Any ideas on how to track this down?

EDIT

p @cities
p @current_location

returns

{:new_york=>[2, 25, 12], :chicago=>[18, 12, 2], :dallas=>[16, 12, 4], :omaha=>[16, 5, 5], :seattle=>[2, 29, 16]}
"new_york"
  • 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-05-24T22:31:08+00:00Added an answer on May 24, 2026 at 10:31 pm

    The error message is saying, that you’re calling [] on nil. Since in the expression @cities[@current_location][num], you’re first calling [] on @cities and then on the result of @cities[@current_location], this means that either @cities or @cities[@current_location] is nil.

    To find out which one is the case, you should trying printing out @cities and @current_location using p @cities, @current_location. If @cities is nil, that’s the reason for the error. If it isn’t it should now at least be apparent why @current_location isn’t a valid key for it.

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

Sidebar

Related Questions

When I run the following code, I get the exception below: ''# NOTE: ExcelApp
When I run the following code to test copying a directory, I get a
I want to run Protovis javascript from Java and get the evaluated SVG code.
When I run the code below when $entry = miami.com, I get the following
I've got the following code that get's a URL parameter, selects an option from
When I run this code I get the following error error: invalid label html_attributions
When I run the code below I get the following error. C:\Documents and Settings\BOS\Desktop\test>java
I run following code and get dialog box for entering password. LNNotesSession session; LNDatabase
when my event fires the following code get's run: WebBrowser browser = new WebBrowser();
I run the AWK code and I get The factorial of 200 is inf

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.