小编Fal*_*ion的帖子

Ruby:跳过获取值

我有一个文件检查和排序脚本.现在我希望用户选择他/她希望如何排序最终输出.可悲的是Ruby似乎忽略了gets命令.如果我注释掉整个部分,脚本就完成了.请忽略def读数.我从未完成那个......

所以我的问题是:为什么Ruby会跳过gets命令.

class Product
  attr_reader :id, :name, :price, :stock
  def initialize(id,name,price,stock)
    @id = id
    @name=name
    @price=price
    @stock=stock
  end
  def readout
    self.each do |product|
      print product.id
      print "|"
      print product.name
      print "|"
      print product.price
      print "|"
      print product.stock
      puts ""
    end
  end
end

products = []
newproducts= []

if ARGV[0] != nil
  if File.exist?(ARGV[0])
    File.open(ARGV[0] , "r") do |f|
      f.each_line do |line|
        products << line
      end
    end
    products.each do |product|
      data = product.split(",")
      newproducts.push(Product.new(data[0].strip, data[1].strip, data[2].strip.to_i, data[3].strip.to_i))
    end

    puts "What to …
Run Code Online (Sandbox Code Playgroud)

ruby sorting file object

4
推荐指数
1
解决办法
604
查看次数

标签 统计

file ×1

object ×1

ruby ×1

sorting ×1