ruby 中 關(guān)于 yield 的用法 誰能給講一下? def fibUpTo(max) i1,i2=1,1 #parallel assignment while i1<=max yield i1 i1, i2 =i2,i1+i2 end end fibUpTo(1000){|f| print f," "}
1 回答

拉丁的傳說
TA貢獻(xiàn)1789條經(jīng)驗(yàn) 獲得超8個(gè)贊
其實(shí)就是一個(gè)占位符, 把塊的內(nèi)容部份粘進(jìn)去,
例如,
def test
yield
end
test do
puts 'this is a test'
end
上面test方法中的yield就是 puts 'this is a test'
- 1 回答
- 0 關(guān)注
- 218 瀏覽