簡體   English   中英

Ruby 1.8.7多行塊

[英]Ruby 1.8.7 multiline blocks

因此,我是Ruby的新手,我試圖編寫一個遍歷文件的多行塊,並執行if語句以獲取文件中每個字符串的最后一行號

i = 0

IO.foreach("testfile.log") {|line|
  if line.include? str1
    x = i
  elsif line.include? str2 and line.include? str3
    y = i
  end

  i++
}

當我嘗試執行腳本時,在代碼塊末尾出現以下錯誤:

錯誤的語法錯誤,意外的'}'

我在這里做錯了什么?

i++不是有效的Ruby。 試試i += 1

如果您需要多行塊,那是不好的形式

i = 0
IO.foreach("testfile.log") do |line|
  if line.include? str1
    x = i
  elsif line.include? str2 and line.include? str3
    y = i
  end
  i+=1 # courtesy of Michael Kohl
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM