2014-06-10 36 views
-2

我有一個哈希:Hash和每個方法

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" 
+0

無法複製。我的代碼沒有無限循環。事實上,你的代碼完全符合你的要求;它只在「a」和「b」內打印範圍。 – sawa

+2

啊... GA的「MTA」作業... ;-) – Pavling

回答

3

它不是無限的,但'Times Square''Union Square'是一個很長的範圍,涉及'Times Squarf','Times Squarg'等等。如果你想獲得第0-4行,它是:

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

謝謝你的工作.. – user3449311