我正在尝试创建一个脚本来遍历索引,查看每个页码,然后告诉我该条目所在的书的哪一章。这是我正在做的事情的近似值:
@chapters = {
1 => "introduction.xhtml",
2..5 => "chapter1.xhtml",
6..10 => "chapter2.xhtml",
11..18 => "chapter3.xhtml",
19..30 => "chapter4.xhtml" }
def find_chapter(number)
@chapters.each do |page_range, chapter_name|
if number === page_range
puts "<a href=\"" + chapter_name + "\page" + number.to_s + "\">" + number.to_s + </a>"
end
end
end
find_chapter(1)
会吐出我想要的字符串,但find_chapter(15)
不返回任何内容。不能像这样使用范围作为键吗?