且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

以下Ruby语法中的“&"是什么意思?

更新时间:2023-01-24 12:45:51

&: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.