Ruby on rails Rails个人类每个例程
在ruby和rails的某些领域,我是个新手。因此,我正在编写一个类,根据扩展名读取excel,并在每个例程中返回行。大概是这样的: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'
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
但这只是一个猜测