Ruby符号和冒号快捷方式在Ruby中,我知道如果我这么做了:some_objects.each(&:foo)就像some_objects.each { |obj| obj.foo }那是,&:foo创建块{ |obj| obj.foo }将其转化为程序,并将其传递给每个人。为什么这样做?这仅仅是Ruby的一个特例,还是有理由这样做呢?
2 回答

呼如林
TA贡献1798条经验 获得超3个赞
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 关注
- 612 浏览
添加回答
举报
0/150
提交
取消