简体   繁体   English

在Ruby中,如何将多个哈希传递给一个方法而没有括号?

[英]In Ruby, how can you pass more than one hash to a method, without parentheses?

In Ruby, how can one pass more than one hash to a method, without parentheses? 在Ruby中,如何将多个哈希传递给一个方法而没有括号?

For example 例如

def abc x,y
end

abc {4,5},{6,4} <-- syntax error, unexpected ',', expecting '}' abc {4,5},{6,4} <- syntax error, unexpected ',', expecting '}'

Mu points out that you can pass multiple hashes without parentheses, if you pass them in as non-literals ie as variables. Mu指出,如果您将非散列形式(即变量形式)传递给多个散列,则可以不带括号而传递多个散列。

But other than that, so, for literal hashes, 但是除此之外,对于文字哈希,

You do(need parentheses to pass multiple literal hashes), unless you are passing keyword arguments.. You can pass multiple keyword arguments without parentheses. 您可以这样做(需要括号以传递多个文字哈希),除非您要传递关键字参数。.您可以传递多个不带括号的关键字参数。

a keyword argument would be when the parameters of the method include a colon like eg def blah x:, y: then you can call with blah y:2,x:3 . 当方法的参数包含冒号(例如def blah x:, y:时,将使用关键字blah y:2,x:3来调用关键字参数。 Sometimes you have to look at the parameter(s) to see if an argument is a keyword argument, eg if you have a method called with abc x:3 then that might be def abc x: in which case you called it with a keyword argument. 有时,您必须查看参数以查看参数是否为关键字参数,例如,如果您有一个用abc x:3调用的方法,则可能是def abc x:在这种情况下,您用关键字对其进行了调用论点。 Or it might be def abc x in which case you called it with a hash, omitting the {}. 或者它可能是def abc x在这种情况下,您使用哈希将其调用,省略了{}。

When I say keyword argument, I don't mean a hash.. and vice versa, when I say hash I mean not a keyword argument. 当我说关键字参数时,我并不是说哈希。反之亦然,当我说哈希时,我的意思不是关键字参数。

When a call is without parentheses, you can only pass one hash and that hash has to be the last argument. 当调用不带括号时,您只能传递一个哈希,并且该哈希必须是最后一个参数。 And you can skip the {} around it. 并且您可以跳过其周围的{}。

note- I'm no expert, but as to a related question of whether a keyword argument is a type of hash, from what I understand, as of writing, pre ruby 3, they are, but there is a proposal for ruby 3 to have 'real' keyword arguments that are distinct from hashes https://bugs.ruby-lang.org/issues/14183 注意-我不是专家,但是关于一个关键词参数是否是哈希类型的相关问题,据我所知,从编写红宝石3之前就可以理解,但是有人建议将红宝石3用于具有不同于哈希https://bugs.ruby-lang.org/issues/14183的 “真实”关键字参数

a keyword argument can't be multi-valued. 关键字参数不能为多值。

also, a hash can be automatically converted to a keyword argument. 同样,哈希可以自动转换为关键字参数。 (eg if a method is defined with a parameter that is a keyword argument, you can pass a hash in eg {x:"a"} , that x being a symbol, and that hash will be converted to a keyword argument x:"a" (that x being a parameter name). (例如,如果使用参数定义的方法是关键字参数,则可以在{x:"a"}传递哈希,即x是符号,并且该哈希将转换为关键字参数x:"a" (x是参数名称)。

I'll add a point regarding blocks and parentheses, because a block done with {} does look a little bit like a hash though is not a hash. 我将添加有关块和括号的要点,因为用{}完成的块看上去有点像哈希,尽管它不是哈希。 And a block can have some influence on whether parentheses are needed. 并且一个块可以影响是否需要括号。

If you see abc {} , that {} is a block not a hash, and blocks don't count as an argument. 如果看到abc {} ,则该{}是一个块,而不是哈希,并且块不算作参数。 Hence that call works for def abc but not for def abc x where one would get an error related to number of arguments passed. 因此,该调用适用于def abc但不适用于def abc x ,因为那里会得到与传递的参数数量有关的错误。

even when passing a block after some other arguments, there should be no comma before the block, and if a block done with {} follows some arguments, you need parentheses, but if a block done with do .. end follows some arguments, you don't need parentheses 即使在其他一些参数之后传递一个块时,在该块之前也不应该有逗号,并且如果用{}完成的块后面有一些参数,则需要括号,但是如果用do .. end完成的块后面有一些参数,则您不需要括号

it is suggested that one use parentheses when one has multiple arguments, unless it's a DSL(domain specific language). 建议除非有DSL(特定于域的语言),否则应在多个参数之间使用括号。 Do you leave parentheses in or out in Ruby? 您是否在Ruby中放入括号?

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

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