我需要写一个识别部分文字红宝石 - 我需要每下一次迭代作为我的方法参数
lines.each do |line|
case line.match
when :chapter
chapter = analyze_chapter(line)
previous = chapter
when :section
section = analyze_section(line)
previous.sections<< section
previous = section
end
end
等,针对不同的元素代码。的analyze_chapter
例子:
def analyze_chapter(chapter_line)
Chapter.new(
title: chapter_line.title,
sections: [analyze_section(chapter_line)]
)
end
问题是我的输入是这样的:
Chapter 1 - name
Section 1 - name
我的代码是如何工作的:我使用正则表达式来识别标题的模式。然后,我把这个匹配的标题保存为Line
对象的title
属性。 它工作正常,在我不同的代码,我在那里认识
Chapter 1 - name, Rest of the text - everything in one line
但很明显,现在我需要下一行传递给sections[analyze_sections(chapter_line)]
- 我种需要下一次迭代,因为Chapter title
和Section title
不是一条线。 我知道这可能是一些逻辑错误。我会很感激任何帮助。
是'analyze_section'和'analyze_sections'应该是同一个方法吗?是属于那一章的章节之后的所有章节? – SteveTurczyn
是的,这是一个错字。是的,章节之后的每个章节都属于它 - 直到下一章出现。 – Jes