输出显示为 null (Ruby)

Output display as null (Ruby)

在这里,当我们打印数组元素时,它始终显示空值,如“[nil, nil, nil, nil]” 值未存储在数组中。

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)

在不使用 class 或实例变量的情况下,我们想要这样做

用户输入

输入航班号

2

输入航班号

2342

输入航班始发地

科钦

输入目的地

tvm

产出

航班详情_

Flight_id :

Flight_number :

Flight_orgin :

Flight_destination:

_航班数组

[无,无,无,无]

@id@num@orgin@destination参数如果不在任何地方设置,则为nil。

因此,当您进行这两个调用时:

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

你基本上只是将 nils 发送到函数中:

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

如果要访问从输入中读取的变量:

  • 首先,您需要将它们存储在某个地方。例如:调用 read_flight 函数时将它们存储在实例变量中。
  • 然后,当你想在数组中推送值时,引用实例变量。

例如:

  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

您可以在此处了解有关 Ruby 的变量范围(实例变量、全局变量等)的更多信息:https://www.techotopia.com/index.php/Ruby_Variable_Scope

您正在构造函数 (def initialize) 中使用 nil 值进行初始化,以修复您可以将值传递给 .new 或更改 read_flight如下:

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

这将修改 class 范围的变量。

或者您可以使用 || 运算符在构造函数中设置默认值(不推荐):

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

首先,要小心,因为你犯了很多小而重要的错误。没关系,我们都是这样开始的) 比如你的'initialize'方法名不正确!
你的:'initilize'
正确:'initialize'
正确命名默认方法很重要。 此外,当您使用方法参数初始化变量时:

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

如果您希望用户初始化您的实例,请不要自行初始化它们,删除初始化方法中的参数。 另外,你可以在整个class中使用实例变量,这真的很有帮助!

所以,我更正了一点:

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

检查:

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

这是我的调整版本:

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

解释:

rubyclass的每个实例可以有尽可能多的实例变量(以@开头)。这些实例变量存在于一个实例中,因此它们在整个方法中保持它们的值。

所以你应该给实例变量赋值,例如:

@id = gets.chomp

然后在另一种方法中使用它:

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

然而,每次我们想要使用实例变量时添加@是非常乏味的。这就是 attr_reader 进来的原因。当你写 attr_reader:

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

您实际上在 Flight 中声明了 4 个方法:

def id
  @id
end

def number
  @number
end

def origin
  @origin
end

def destination 
  @destination 
end

然后你可以只使用 id, number, origin, destination 而没有前导 @`