ibz95

Joined

50 Experience
0 Lessons Completed
0 Questions Solved

Activity

Hi guys,
I have written the following code in Ruby that takes input from a user regarding plane and flight details and displays the user inputs as an array in the terminal. For example, if the user enters 3 then the program reads in input 3 times. However, I am also getting the following error:
Traceback (most recent call last): 2: from plane.rb:68:in '
1: from plane.rb:65:inmain' plane.rb:56:in print_planes': undefined methodlength' for nil:NilClass (NoMethodError)

require './input_functions'
class Plane
    attr_accessor :id, :number, :origin, :destination

    def initialize (id, number, origin, destination)
        @id=id
        @number = number
        @origin = origin
        @destination = destination
    end 
end 
def read_plane()

    plane_id = read_integer("Enter plane id:")
    plane_number = read_string("Enter flight name:")
    plane_origin = read_string("Enter origin airport:")
    plane_destination = read_string("Enter destination airport:")
    plane = Plane.new()
    plane.id = plane_id
    plane.number = plane_number
    plane.origin = plane_origin
    plane.destination = plane_destination   
    return plane
end

def read_planes()
    planes=Array.new()
    count=0
    while (count<4)
        planes<<read_plane()
        count+=1
    end     
end

def print_plane(plane)
    puts ("Plane id "+plane.id)
    puts ("Flight "+plane.number)
    puts ("Origin "+plane.origin)
    puts ("Destination "+plane.destination)

end

def print_planes(planes)
    index = 0
    while (index<planes.length)
        puts planes[index]
        index+=1
    end 

end

def main()
    planes = read_planes()
    print_planes(planes)
end

main()
logo Created with Sketch.

Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

© 2023 GoRails, LLC. All rights reserved.