我正在尝试创建一个脚本来通过索引,查看每个页面的数字,并告诉我这本书的哪一个章节.这是我在做什么的近似:
@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)不返回任何东西.是不是可以使用一个范围作为这样的关键?