简体   繁体   English

Ruby Proc语法用法

[英]Ruby Proc syntax usage

my_proc = proc{|x| "this is #{x}"}

given my_proc , what makes the following syntax work? 给定my_proc ,什么使以下语法起作用?

my_proc.call("x") # makes sense

my_proc.("x") # not really sure but ok

my_proc["x"] # uhhh....

my_proc === "x" # what the deuce?!

About === : 关于===

http://ruby-doc.org/core-2.2.0/Proc.html#method-i-3D-3D-3D http://ruby-doc.org/core-2.2.0/Proc.html#method-i-3D-3D-3D

proc === obj → result_of_proc proc === obj→result_of_proc

Invokes the block with obj as the proc's parameter like #call. 使用obj作为proc的参数(如#call)调用该块。 It is to allow a proc object to be a target of when clause in a case statement. 它允许proc对象成为case语句中when子句的目标。

That means you can use it in case statements, like this: 这意味着您可以在case语句中使用它,如下所示:

odd = proc { |x| x % 2 != 0 }
even = proc { |x| x % 2 == 0 }
case 1
when odd then 'odd'
when even then 'even'
end
# => "odd"

Ruby often has several syntaxes for the same method, to best fit the develloper needs. Ruby通常针对同一方法具有多种语法,以最适合develloper的需求。

  • my_proc === "x" : http://ruby-doc.org/core-2.2.0/Proc.html#method-i-3D-3D-3D -> this one is said to be useful in case statements (@Marek_Lipka explained it further in his answer) my_proc === "x"http : my_proc === "x" >据说这在case语句中很有用( @Marek_Lipka在回答中进一步解释了这一点)
  • my_proc["x"] : http://ruby-doc.org/core-2.2.0/Proc.html#method-i-5B-5D -> This one is said to be "syntax sugar", hiding the method's name for a more compact syntax. my_proc["x"]http : my_proc["x"] >据说这是“语法糖”,隐藏了方法的名称以获得更紧凑的语法。

Since you are specifically asking about the syntax, this has nothing to do with Proc s. 由于您是专门询问语法的,因此与Proc无关。 Ruby doesn't allow objects to change the syntax of the language, therefore it doesn't matter what kind of objects we are talking about. Ruby不允许对象更改语言的语法,因此我们在讨论哪种对象都没有关系。

my_proc.call("x")

This is just standard message sending syntax. 这只是标准消息发送语法。 It sends the message call with the argument "x" to the object returned by evaluating the expression my_proc . 它将带有参数"x"的消息call发送到通过评估表达式my_proc返回的对象。

You are asking "what makes this syntax work". 您在问“什么使此语法起作用”。 Well, this is just how message sending is specified in the Ruby Language Specification. 好的,这就是在Ruby Language Specification中指定消息发送的方式。

my_proc.("x")

This is syntactic sugar for my_proc.call("x") , ie exactly what we had above: sending the message call with argument "x" to the result of evaluating my_proc . 这是my_proc.call("x")语法糖,即我们上面的内容:将带有参数"x"的消息call发送到评估my_proc的结果。

If you want to make this work for your objects, you need to respond to call . 如果您想使此功能适用于您的对象,则需要响应call

This syntax was added in Ruby 1.9 to make calling a "function-like object" look more like sending a message, with the only difference being the additional period character. 在Ruby 1.9中添加了此语法,使调用“类似函数的对象”看起来更像是发送消息,唯一的区别是附加的句点字符。 Note that Ruby is not the only language using this syntax, uses it as well. 请注意,Ruby不是唯一使用此语法的语言,而使用它。

my_proc["x"]

This is syntactic sugar for my_proc.[]("x") , ie sending the message [] with argument "x" to the result of evaluating my_proc . 这是my_proc.[]("x")语法糖,即,将带有参数"x"的消息[]发送到评估my_proc的结果。

If you want to make this work for your objects, you need to respond to [] . 如果要使此功能适用于您的对象,则需要响应[]

Proc#[] was added as an alias_method of Proc#call , so that calling a "function-like object" looks more like sending a message, with the only difference being the shape of the brackets. Proc#[]被添加为Proc#callalias_method ,因此,调用“类似于函数的对象”看起来更像是发送消息,唯一的区别在于括号的形状。 With the addition of the .() syntax sugar in Ruby 1.9, I generally prefer that one. 在Ruby 1.9中添加了.()语法糖后,我通常更喜欢使用它。

my_proc === "x"

This is syntactic sugar for my_proc.===("x") , ie sending the message === with argument "x" to the result of evaluating my_proc . 这是my_proc.===("x")语法糖,即,将带有参数"x"的消息===发送到评估my_proc的结果。

If you want to make this work for your objects, you need to respond to === . 如果要使此功能适用于您的对象,则需要响应===

This was added so that Proc s could be used as conditions in case expressions and in Enumerable#grep , both of which use === to determine whether or not an object could be subsumed unter a category. 添加它是为了使Proc可以用作case表达式和Enumerable#grep ,两者都使用===来确定对象是否可以归入类别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM