Home > Mobile >  Output display as null (Ruby)
Output display as null (Ruby)

Time:11-30

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]

CodePudding user response:

The @id, @num, @orgin, @destination parameters will be nil if you don't set them anywhere.

So when you make these two calls:

input_flight.print_flight(@id, @num, @orgin, @destination)
input_flight.read_flights(@id, @num, @orgin, @destination)

You basically just send nils into the function:

input_flight.print_flight(nil, nil, nil, nil)
input_flight.read_flights(nil, nil, nil, nil)

If you want to access the variables read from the input:

  • First, you need to store them somewhere. For ex: store them inside the instance variables when read_flight function is called.
  • Then, refer the instance variable when you want to push values in the array.

Ex:

  def read_flight
    puts "enter flight id"
    @id = gets.chomp # store inside instance variable
    puts "enter flight number"
    @number = gets.chomp
    puts "enter flight origin location"
    @origin = gets.chomp
    puts "enter destination"
    @destination = gets.chomp
  end
  
  def read_flights
    ...
    flightid.push(@id, @number, @origin, @destination) # access instance variables here
    ...
  end

You can learn more about Ruby's variable scoping (instance variables, global variables, etc) here: https://www.techotopia.com/index.php/Ruby_Variable_Scope

CodePudding user response:

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 @`

CodePudding user response:

You're initializing with nil values in your constructor (def initialize), to fix that you can pass the values to the .new or change the read_flight as follows:

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

This will modify the class-scoped variables.

Or alternatively you can have default values in the constructor (not recommended) using the || operator:

def initilize(flight_id, flight_num, flight_orgin, flight_destination)
  @id= flight_id || 0
  @number = flight_number || 0
  @origin = flight_origin || ""
  @destination = flight_destination || ""
end

CodePudding user response:

First of all, be careful, because you did a lot of little but important mistakes. It's ok, we all started like that) For example, your 'initialize' method name is not correct!
Your: 'initilize'
Correct: 'initialize'
It's important to name default methods correct. Also, when you initialize variables with method arguments:

def initilize(flight_id, flight_num, flight_orgin, flight_destination)
    @id= flight_id
    @number = flight_num #you have to name it just like argument in method not flight_number, because it does not exist at all
    @origin = flight_origin #same here, you forgot one letter
    @destination = flight_destination
  end

If you want user to initialize your instances then don't initialize them by yourself, delete arguments in initialize method. Also, you can use instance variables in entire class, it's really helpful!

So, i corrected a little:

class Flight

  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.to_s
    puts "Flight_number     : "   @number.to_s
    puts "Flight_origin      : "   @origin
    puts "Flight_destination: "   @destination
  end
  def read_flights
    puts "_______Array of flights______"
    flightid = Array.new
    flightid.push({ @id,@number,@origin,@destination })
    puts "#{flightid}"
  end
end

Check:

   input_flight = Flight.new
   input_flight.read_flight
   input_flight.print_flight
   input_flight.read_flights
  • Related