Đối với chỉ là cú pháp đường.
For ... In
Earlier we said that the only built-in Ruby looping primitives were while and until. What's this ``for'' thing, then? Well, for is almost a lump of syntactic sugar. When you write
for aSong in songList
aSong.play
end
Ruby translates it into something like:
songList.each do |aSong|
aSong.play
end
The only difference between the for loop and the each form is the scope of local variables that are defined in the body. This is discussed on page 87.
You can use for to iterate over any object that responds to the method each, such as an Array or a Range.
for i in ['fee', 'fi', 'fo', 'fum']
print i, " "
end
for i in 1..3
print i, " "
end
for i in File.open("ordinal").find_all { |l| l =~ /d$/}
print i.chomp, " "
end
produces:
fee fi fo fum 1 2 3 second third
As long as your class defines a sensible each method, you can use a for loop to traverse it.
class Periods
def each
yield "Classical"
yield "Jazz"
yield "Rock"
end
end
periods = Periods.new
for genre in periods
print genre, " "
end
produces:
Classical Jazz Rock
Ruby không có các từ khóa khác cho comprehensions danh sách (như ví dụ tổng bạn đã thực hiện ở trên). for
không phải là từ khóa phổ biến khủng khiếp và cú pháp phương pháp (arr.each {}
) thường được ưu tiên.
Wow, rất tinh tế, nhưng sẽ trở nên tiện dụng khi tôi chạy vào một cái gì đó như thế này. Cảm ơn! – sivabudh
Trên thực tế ví dụ thứ hai của bạn ném 'NameError: biến không xác định cục bộ hoặc phương thức 'i' cho chính: Object'. Đó là bởi vì bạn đang thiếu 'do'. –
@JakubHampl Tôi đã sửa nó. Cảm ơn! –