Search code examples
arraysrubylocal-variables

Output display as null (Ruby)


Here when we print array elements it display null value all time like "[nil, nil, nil, nil]" Values are not getting stored in array.

class Flight
  def initilize(flight_id, flight_num, flight_orgin, flight_destination)
    @id= flight_id
    @number = flight_number
    @origin = flight_origin
    @destination = flight_destination
  end

  def read_flight()
    puts "enter flight id"
    flight_id = gets.chomp
    puts "enter flight number"
    flight_number = gets.chomp
    puts "enter flight origin location"
    flight_origin = gets.chomp
    puts "enter destination"
    flight_destination = gets.chomp
  end
  def print_flight(id, number, orgin, destination)
    puts "_____Flight details______"
    puts "Flight_id         :#{id}"
    puts "Flight_number     :#{number}"
    puts "Flight_orgin      :#{orgin}"
    puts "Flight_destination:#{destination}"
  end
  def read_flights(id, number, orgin, destination)
    puts "_______Array of flights______"
    flightid = Array.new
    flightid.push(id, number, orgin, destination)
    puts "#{flightid}"
  end
end
input_flight = Flight.new
input_flight.read_flight
input_flight.print_flight(@id, @num, @orgin, @destination)
input_flight.read_flights(@id, @num, @orgin, @destination)

Without using a class or instance variable we want to do it

User input

enter flight id

2

enter flight number

2342

enter flight origin location

cochin

enter destination

tvm

output

Flight details_

Flight_id :

Flight_number :

Flight_orgin :

Flight_destination:

_Array of flights

[nil, nil, nil, nil]


Solution

  • Here is my version of adjustion:

    class Flight
      attr_reader :id, :number, :origin, :destination
    
      def read_flight
        puts "enter flight id"
        @id = gets.chomp
        puts "enter flight number"
        @number = gets.chomp
        puts "enter flight origin location"
        @origin = gets.chomp
        puts "enter destination"
        @destination = gets.chomp
      end
    
      def print_flight
        puts "_____Flight details______"
        puts "Flight_id         :#{id}"
        puts "Flight_number     :#{number}"
        puts "Flight_orgin      :#{origin}"
        puts "Flight_destination:#{destination}"
      end
    
      def read_flights
        puts "_______Array of flights______"
        flightid = [id, number, origin, destination]
        puts "#{flightid}"
      end
    end
    input_flight = Flight.new
    input_flight.read_flight
    input_flight.print_flight
    input_flight.read_flights
    

    Explanation:

    Each instance of ruby class can have as many instance variables (which begin with @) as possible. Those instance variables live in an instance so they keep their value across the methods.

    So you should assign the value you want to instance variables, for example:

    @id = gets.chomp
    

    then use it in another method:

    def print_flight
        puts "_____Flight details______"
        puts "Flight_id         :#{@id}"
    end
    

    However, add @ everytime we want to use the instance variables is pretty tedious. That's why attr_reader comes in. When you write attr_reader:

    attr_reader :id, :number, :origin, :destination 
    

    You actually declare 4 methods inside Flight:

    def id
      @id
    end
    
    def number
      @number
    end
    
    def origin
      @origin
    end
    
    def destination 
      @destination 
    end
    

    Then you can just use id, number, origin, destination without the leading @`