哈希和每种方法

时间:2014-06-10 23:22:14

标签: ruby

我有一个哈希:

lines = {
   :n => ["Times Square", "34th", "28th", "23rd", "Union Square", "8th"],

当我这样做时:

first_line = lines[:n]
a = first_line[0]
b = first_line[4]
(a..b).select{|x| puts x}

我达到了超限。是否有方法只打印从ab的范围?

大家好,你可以停止让我失望了... :)我忘了约会我的回答...... BTW上述解决方案的另一种方式是

  lines = {
 :n => ["Times Square", "34th", "28th", "23rd", "Union Square", "8th"}

 puts first_line = lines[:n]
 puts first_line.slice(0, 5) => "Times Square", "34th", "28th", "23rd",  "Union Square"

1 个答案:

答案 0 :(得分:3)

它不是无限的,但是'Times Square''Union Square'是一个非常长的范围,涉及'Times Squarf''Times Squarg',依此类推。如果你想得到0-4行,那就是:

lines[:n][0..4].each { |x| puts x }