簡體   English   中英

Ruby:塊參數中的意外分號

[英]Ruby: unexpected semicolon in block parameters

我剛剛開始學習Ruby。 我輸入了示例

x = 10
5.times do |y; x|
  x = y
  puts "x inside the block: #{x}"
end
puts "x outside the block: #{x}"

我有一個錯誤:

hello.rb:3:語法錯誤,意外的“;”,預期為“ |” 5.times | y; x |

請向我解釋是什么意思? 據我所知,此代碼應該可以工作。

那是一個新的1.9結構 ,您正在使用1.8。


它也適用於lambda(包括刺入),這很不錯:

> x = 42
> love_me = ->(y; x) do
*   x = y
*   puts "x inside the block: #{x}"
* end
> 2.times &love_me
x inside the block: 0
x inside the block: 1
> puts "x outside the block: #{x}"
x outside the block: 42

暫無
暫無

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

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