Ruby 过程语法
我昨天在这里提出的问题的答案是以下 Ruby 代码:
def overlap?(r1,r2)
r1.include?(r2.begin) || r2.include?(r1.begin)
end
def any_overlap?(ranges)
ranges.sort_by(&:begin).each_cons(2).any? do |r1,r2|
overlap?(r1, r2)
end
end
我得到了 each_cons
,但是奇怪的 &:begin
表示法是什么?把我从语法地狱中救出来!
谢谢!
An answer to a question I posed yesterday on here was the following piece of Ruby code:
def overlap?(r1,r2)
r1.include?(r2.begin) || r2.include?(r1.begin)
end
def any_overlap?(ranges)
ranges.sort_by(&:begin).each_cons(2).any? do |r1,r2|
overlap?(r1, r2)
end
end
I get each_cons
, but what's the strange &:begin
notation? Save me from syntax hell!
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您在调用的最后一个参数前加上
&
前缀时,您就明确表示您正在发送一个块,而不是一个正常参数。好的,在method(&:something)
中,:something
是一个符号,而不是 proc,因此 Ruby 会自动调用该方法to_proc
获取真正的块。 Rails 人员(现在还有普通 Ruby)巧妙地将其定义为:这就是为什么你可以这样做:
[编辑] 注意:当你意识到这种构造不是语法糖而是 Ruby 提供的通用基础设施时,没有什么可以阻止你实现你自己的其他类的
to_proc
。从未因为&:method
不允许任何参数而感到受限?When you prefix the last argument of a call with
&
you are making clear that you are sending a block and not a normal argument. Ok, inmethod(&:something)
,:something
is a symbol, not a proc, so Ruby automatically calls the methodto_proc
to get a real block. And Rails guys (and now also vanilla Ruby) cleverly defined it as:That's why you can do:
[edit] Note: when you realize that this construction is no syntatic sugar but generic infrastructure that Ruby provides, nothing stops you from implementing your own
to_proc
for other classes. Never felt limited because&:method
allowed no arguments?my_method(&some_value)
表示调用my_method
,在特殊参数槽(proc-slot)中传递some_value
,通常保留用于传递 do - 符号块。任何
Proc
或响应to_proc
的对象都允许在 proc-slot 中传递。如果您传入一个不是Proc
但响应to_proc
的对象,那么 Ruby 将为您调用该对象上的to_proc
并传递将结果放入方法中。Symbol#to_proc
的实现是返回一个 proc,当传递一个参数时,该 proc 会向该参数发送符号本身的消息。例如,:hello.to_proc.call(my_obj)
最终将执行my_obj.send :hello
。因此
my_array.each(&:hello)
将:hello
传递给 proc-slot 中的each
(通常会传递一个块,如果你使用了 do 符号来创建一个块)。:hello.to_proc.call(my_array[0])
最终成为my_array[0].send :hello
,对于my_array 的所有后续索引也是如此
。my_method(&some_value)
means to invokemy_method
, passingsome_value
in the special argument slot, the proc-slot, usually reserved for passing do-notation blocks.Any object which is a
Proc
or which responds toto_proc
is permitted to be passed in the proc-slot. If you pass in an object which is not aProc
but which responds toto_proc
, then Ruby will callto_proc
on the object for you and pass the result into the method.The implementation of
Symbol#to_proc
is to return a proc which, when passed an argument, sends that argument the message that is the symbol itself. For example,:hello.to_proc.call(my_obj)
will end up doingmy_obj.send :hello
.So
my_array.each(&:hello)
passes:hello
toeach
in the proc-slot (where a block would normally passed, if you used the do-notation to make a block).:hello.to_proc.call(my_array[0])
ends up beingmy_array[0].send :hello
, and the same for all subsequent indexes ofmy_array
.它等于:
it equals: