Ruby 方法定义语法与 &关键词
class ExchangeUtil
def exchange_to(other_currency, date = Date.current, &)
@bank.exchange_with_on(self, other_currency, date, &)
end
end
在方法定义和方法调用中, &amp& 的siginciencience是什么?
class ExchangeUtil
def exchange_to(other_currency, date = Date.current, &)
@bank.exchange_with_on(self, other_currency, date, &)
end
end
What is the siginificance of & in method definition and method calls?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是 Ruby 3.1 中的一项新功能:匿名块参数
这 只是当您有一个函数的块参数时的快捷方式,该函数的唯一目的是传递给另一个函数。较旧的语法是
参数
&block
可以有任何名称,例如&my_block
,但该名称通常毫无意义,所以他们做了它“匿名的”。It's a new feature in Ruby 3.1: Anonymous block argument
It's just a shortcut for when you have a block argument for a function whose only purpose is to be passed to another function. Older syntax for that would've been
The param
&block
could've had any name, like&my_block
, but the name was generally meaningless, so they made it "anonymous".