作者suomax (shadow)
看板Ruby
标题[问题] 问个语法问题
时间Mon Sep 1 01:03:40 2008
arr = [1, 2, 3]
arr.map(&:to_s)
# => ["1", "2", "3"]
arr.map(&:class)
# => [Fixnum, Fixnum, Fixnum]
大概可以看出这样的语法等同於
arr.map {|f| f.to_s} / arr.map {|f| f.class}
只是我不太懂为甚麽可以作这样的 shortcut?
这是本来就有的功能,还是可以从语法的层面解释呢?
谢谢 m(_ _)m
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 59.117.1.160
1F:→ suomax:自问自答 是因为 ActiveSupport 替 Symbol 加上 to_proc 09/01 01:46
※ 编辑: suomax 来自: 59.117.1.160 (09/01 01:46)