简体   繁体   English

(一元)*运算符在此Ruby代码中做什么?

[英]What does the (unary) * operator do in this Ruby code?

Given the Ruby code 给定Ruby代码

line = "first_name=mickey;last_name=mouse;country=usa" 
record = Hash[*line.split(/=|;/)] 

I understand everything in the second line apart from the * operator - what is it doing and where is the documentation for this? 除了*运算符外,我在第二行中了解所有内容-它在做什么,相关文档在哪里? (as you might guess, searching for this case is proving hard...) (您可能会猜到,搜索这种情况非常困难...)

The * is the splat operator. *splat运算符。

It expands an Array into a list of arguments, in this case a list of arguments to the Hash.[] method. 它将Array扩展为参数列表,在本例中为Hash.[]方法的参数列表。 (To be more precise, it expands any object that responds to to_ary / to_a , or to_a in Ruby 1.9.) (更确切地说,它膨胀的任何对象响应to_ary / to_a ,或to_a Ruby 1.9中)。

To illustrate, the following two statements are equal: 为了说明这一点,以下两个语句相等:

method arg1, arg2, arg3
method *[arg1, arg2, arg3]

It can also be used in a different context, to catch all remaining method arguments in a method definition. 它也可以在不同的上下文中使用,以捕获方法定义中所有剩余的方法参数。 In that case, it does not expand, but combine: 在这种情况下,它不会扩展,而是结合在一起:

def method2(*args)  # args will hold Array of all arguments
end

Some more detailed information here . 这里有一些更详细的信息

The splat operator unpacks an array passed to a function so that each element is sent to the function as an individual parameter. splat运算符解压缩传递给函数的数组,以便将每个元素作为单独的参数发送给函数。

A simple example: 一个简单的例子:

>> def func(a, b, c)
>>   puts a, b, c
>> end
=> nil

>> func(1, 2, 3)  #we can call func with three parameters
1
2
3
=> nil

>> list = [1, 2, 3]
=> [1, 2, 3]

>> func(list) #We CAN'T call func with an array, even though it has three objects
ArgumentError: wrong number of arguments (1 for 3)
    from (irb):12:in 'func'
    from (irb):12

>> func(*list) #But we CAN call func with an unpacked array.
1
2
3
=> nil

That's it! 而已!

As everyone mentions, it's a "splat". 众所周知,这是一个“摔角”。 Looking for Ruby syntax is impossible, and I've asked this in other questions. 寻找Ruby语法是不可能的,而我在其他问题中也曾问过。 The answer to that part of the question is that you search on 该部分问题的答案是您搜索

asterisk in ruby syntax

in Google. 在Google中。 Google is there for you, just put what you see into words. Google在这里为您服务,只需将您看到的内容说出来即可。

Anyhoo, like a lot of Ruby code, that code is quite dense. 就像很多Ruby代码一样,Anyhoo的代码非常密集。 The

line.split(/=|;/)

makes an array of SIX elements, first_name, mickey, last_name, mouse, country, usa . 制作一个由SIX元素组成的数组,这些元素包括first_name, mickey, last_name, mouse, country, usa Then the splat is used to make that into a Hash. 然后使用splat将其制作为哈希。 Now the Ruby people always send you to look at the Splat method, since everything is exposed in Ruby. 现在,由于所有内容都在Ruby中公开,因此Ruby人员总是会送您去查看Splat方法。 I have no idea where it is, but once you have that, you'll see that it runs a for through the array and builds the hash. 我不知道它在哪里,但是一旦有了它,您就会看到它在数组中运行for并生成哈希。

You would look for the code in the core documentation. 您将在核心文档中查找代码。 If you cannot find it (I could not), you would try to write some code like this (which works, but is NOT Ruby-like code): 如果找不到(我找不到),则尝试编写这样的代码(可以,但是不是类似Ruby的代码):

line = "first_name=mickey;last_name=mouse;country=usa"
presplat = line.split(/=|;/)
splat = Hash.new
for i in (0..presplat.length-1)
    splat[presplat[i]] = presplat[i+1] if i%2==0
end

puts splat["first_name"]

and then the Ruby gang will be able to tell you why your code is silly, bad, or just plain wrong. 然后Ruby帮派就能告诉您您的代码为何愚蠢,糟糕或完全错误。

If you've read this far, take a read through the Hash documentation for initialization. 如果您已读过本文,请通读Hash文档以进行初始化。

Basically a hash that is initialized with several arguments creates them as key value pairs: 基本上,使用几个参数初始化的哈希将其创建为键值对:

Hash["a", 100, "b", 200] #=> {"a"=>100, "b"=>200}

So in your example this would lead to the following Hash: 因此,在您的示例中,这将导致以下哈希:

{"first_name"=>"mickey", "last_name"=>"mouse", "county"=>"usa"}

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

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