【问题标题】:Output display as null (Ruby)输出显示为空(Ruby)
【发布时间】:2022-01-06 02:18:43
【问题描述】:

在这里,当我们打印数组元素时,它始终显示空值,例如“[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)

不使用类或实例变量我们想要这样做

用户输入

输入航班号

2

输入航班号

2342

输入航班始发地

科钦

输入目的地

电视

输出

航班详情_

Flight_id:

航班号:

Flight_orgin:

Flight_destination:

_航班数组

[无,无,无,无]

【问题讨论】:

  • read_flights 的预期结果是什么?
  • 航班详情_ Flight_id :2 Flight_number :2342 Flight_orgin :Cochin Flight_destination:tvm _Array of flight [2, 2342, Cochin, tvm]

标签: arrays ruby local-variables


【解决方案1】:

如果您不在任何地方设置@id@num@orgin@destination 参数,它们将为零。

所以当你拨打这两个电话时:

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

【讨论】:

    【解决方案2】:

    这是我的调整版本:

    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
    

    解释:

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

    所以你应该将你想要的值赋给实例变量,例如:

    @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而不用前导@`

    【讨论】:

      【解决方案3】:

      您在构造函数 (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
      

      这将修改类范围的变量。

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

      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
      

      【讨论】:

      • o/p 相同,为空
      【解决方案4】:

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

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

      【讨论】:

      • 是的,它奏效了。谢谢大佬
      • 您的initialize 方法的目的是什么?它什么也没做。它取消引用@id(这只是nil,因为实例变量没有初始化),然后它完全忽略结果,然后它取消引用@number(这只是nil,因为实例变量没有初始化),然后它完全忽略结果,然后它取消引用@origin(这只是nil,因为实例变量没有初始化),然后它完全忽略结果,...
      • ... 然后它取消引用@destination(这只是nil,因为实例变量没有初始化),然后它完全忽略了结果。换句话说:它根本不做任何事情。它可以完全删除。
      猜你喜欢
      • 2014-05-07
      • 2010-10-23
      • 2021-01-14
      • 1970-01-01
      • 2015-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-30
      相关资源
      最近更新 更多