Ruby符號(hào)和冒號(hào)快捷方式在Ruby中,我知道如果我這么做了:some_objects.each(&:foo)就像some_objects.each { |obj| obj.foo }那是,&:foo創(chuàng)建塊{ |obj| obj.foo }將其轉(zhuǎn)化為程序,并將其傳遞給每個(gè)人。為什么這樣做?這僅僅是Ruby的一個(gè)特例,還是有理由這樣做呢?
2 回答

呼如林
TA貢獻(xiàn)1798條經(jīng)驗(yàn) 獲得超3個(gè)贊
class Regexp def to_proc ->(str) { self =~ str ; $1 } endend%w(station nation information).map &/(.*)ion/=> ["stat", "nat", "informat"]
class Integer def to_proc ->(arr) { arr[self] } endendarr = [[*3..7],[*14..27],[*?a..?z]]arr.map &4=> [7, 18, "e"]
arr.map(&:fifth)
arr.map &4
?
- 2 回答
- 0 關(guān)注
- 623 瀏覽
添加回答
舉報(bào)
0/150
提交
取消