Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails个人类每个例程_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails Rails个人类每个例程

Ruby on rails Rails个人类每个例程,ruby-on-rails,ruby,Ruby On Rails,Ruby,在ruby和rails的某些领域,我是个新手。因此,我正在编写一个类,根据扩展名读取excel,并在每个例程中返回行。大概是这样的: class ExcelRead (dependencies) def initialize(path, sheet_n = 0) type = File.extname(path) if type == JitExcelRead::XLS Spreadsheet.client_encoding = 'UTF-8'

在ruby和rails的某些领域,我是个新手。因此,我正在编写一个类,根据扩展名读取excel,并在每个例程中返回行。大概是这样的:

class ExcelRead
  (dependencies)

  def initialize(path, sheet_n = 0)
    type = File.extname(path)

    if type == JitExcelRead::XLS
      Spreadsheet.client_encoding = 'UTF-8'
      book = Spreadsheet.open path
      book_sheet = book.worksheet sheet_n
    elsif type == JitExcelRead::XLSX
      book = Creek::Book.new path
      book_sheet = book.sheets[sheet_n]
    end

    @book = book
    @book_sheet = book_sheet
    @book_rows = book_sheet.rows
    @path = path
    @type = type
  end
end
所以这意味着我需要我的申请

xls = ExcelRead.new(uploaded_file.filename_path)
一切都很顺利。我有我需要的东西可以支配。我现在的问题是如何遍历它们。我认为像这样给类添加一个方法

  def each
    binding.pry
  end
在我的应用程序上正常调用它,就像这样

xls.book_rows.each do |row|
end
会让我输入密码,但不是真的


帮助?

如果您在
ExcelRead
类中添加了
each
方法,并创建了名为
xls
的此类实例,则必须使用
xls.each
,而不是
xls.book\u行访问该类。each

使用前者,您可以从
枚举器中调用
each
方法,因为
book\u行
是一个集合。
我只能猜测您想要一种自定义的方式来迭代您的
图书行
,因此我认为您应该尝试实现这样的目标:

def iterate
  self.book_rows.each do |br|
    # do stuff
  end
end
你这样称呼它:

xls.iterate
但这只是一个猜测