Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

267
Views
Code doesn't run when I had one more elsif statement

Can anyone please explain to me why my code doesn't run when I have my second elsif statement in. I'm sure it's something simple but I've been over it a few times, wrote out the code again and still can't work out the bug. It only bugs when line 25 to 30 are in and says

undefined method`[]' on line 35

but this error will change to something else if I run it again.

So this is affecting line_three as a test I am trying "Southern Cross" for the starting location and "Windsor" for the destination.

This is an error message I receive:

Traceback (most recent call last):
pt_planner.rb:35:in `<main>': undefined method `[]' for nil:NilClass (NoMethodError)

another is:

Traceback (most recent call last):
pt_planner.rb:33:in `<main>': undefined method `-' for nil:NilClass (NoMethodError)

Hope this edit helps

require 'pry'

line_one = ["flinders street", "richmond", "east richmond", "burnley", "hawthorn", "glenferrie"]
line_two = ['Flagstaff', 'Melbourne Central', 'Parliament', 'Richmond', 'Kooyong', 'Tooronga']
line_three = ['Southern Cross', 'Richmond', 'South Yarra' ,'Prahran' 'Windsor']

#1 where would you like to go capture input
puts "what is your starting location?"
user_input_1 = gets.chomp
puts "what is your end location"
user_input_2 = gets.chomp


if line_one.include?(user_input_1) == true and 
    line_one.include?(user_input_2) == true
    origin_index = line_one.index(user_input_1)
    destination_index = line_one.index(user_input_2) 
    route = line_one
elsif  
    line_two.include?(user_input_1) == true and 
    line_two.include?(user_input_2) == true
    origin_index = line_two.index(user_input_1)
    destination_index = line_two.index(user_input_2)
    route = line_two
elsif
    line_three.include?(user_input_1) == true and
    line_three.include?(user_input_2) == true
    origin_index = line_three.index(user_input_1)
    destination_index = line_two.index(user_input_2)
    route = line_three
end

stops = destination_index - origin_index

puts "origin: #{route[origin_index]}\ndestination: #{route[destination_index]}\n#{route.slice(origin_index +1, stops)}\n stops total: #{destination_index - origin_index}"

binding.pry
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You tried to search in wrong array

Change

destination_index = line_two.index(user_input_2)

to

destination_index = line_three.index(user_input_2)

Also you don't need to compare with true. include? returns true or false, that's enough. It's better to use && instead of and. And put condition on one line with elsif

You can refactor like this to check both values are in the array

([user_input_1, user_input_2] - line_three).empty?
over 4 years ago · Santiago Trujillo Report

0

In your line_three Array, you have forgotten the comma between 'Prahran' and 'Windsor'. As such, Ruby parses this as a string continuation and adds a single element as 'PrahranWindsor' here.

With that, none of your if or elsif conditions match. Consequently, note of the variables you set on any of the branches will be set and instead will be implicitly initialized as nil. As you assume these variables to be set later in your program, things break.

To fix this, you should at first fix the definition of your line_three array.

You should also add code to handle the case that none of your queries matched. Here, you could add a final else branch which e.g. shows an error message and asks the user to try again.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!