[導(dǎo)入]Ruby中的函數(shù)指針
最近忙啊,一直到18號才能有半天休假,也沒時間系統(tǒng)的學(xué)習(xí)了,抽時間看看Programming Ruby 2,·寫一些簡單的例子好了。這個“函數(shù)指針”在C語言里面也可以實(shí)現(xiàn),當(dāng)然,在Ruby中顯得更簡單 def add(a, b)
a+b end def minus(a, b)
a-b end def calc(who, a, b)
who.call(a ,b) end m = method(:add)
puts calc(m, 7,3) #result = 10 m = method(:minus) puts calc(m, 7,3) #result = 4 |
文章來源: http://underwind.javaeye.com/blog/57276