以下Ruby语法中的“&"是什么意思? [英] What does the `&` mean in the following ruby syntax?

查看:96
本文介绍了以下Ruby语法中的“&"是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的ruby示例中,&代表什么?是沿着+=的线循环吗?

In the following ruby example, what does the & represent? Is it along the line of += in a loop?

payments.sum(&:price)

谢谢

丰富

推荐答案

&:price是在集合的每个成员上使用#price方法"的简写.

&:price is shorthand for "use the #price method on every member of the collection".

一元&"作为参数传递给方法时,告诉Ruby接受并将其转换为Proc".符号上的#to_proc方法将#将该符号发送到接收对象,接收对象使用该名称调用相应的方法.

Unary "&", when passed as an argument into a method tells Ruby "take this and turn it into a Proc". The #to_proc method on a symbol will #send that symbol to the receiving object, which invokes the corresponding method by that name.

这篇关于以下Ruby语法中的“&"是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆