splat net
by Declan Meehan
由Declan Meehan
Ruby的* Splat和double ** Splat运算符简介 (An introduction to Ruby’s *Splat and double **Splat operators)
Have you ever wanted to define a method without knowing how many arguments it will take? Do you spend long restless nights wishing there was an easy way to separate a list into a hash? Well look no further than Ruby’s splat operators! There are so many great things you can do with these, but I’m just going to go over the basics plus a few neat tricks I’ve discovered.
您是否曾经想过在不知道需要多少个参数的情况下定义一个方法? 您是否花费了漫长的不眠之夜,希望有一种简单的方法可以将列表分成哈希? 好吧,没有比Ruby的splat运算符更好的了! 您可以用这些东西做很多事情,但是我将介绍一些基础知识以及一些我发现的巧妙技巧。
单* Splat (Single *Splat)
The splat operator has almost endless uses. But the main idea is that whenever you don’t want to specify the number of arguments you have, you would use a splat operator. The simplest example would be something like this:
splat运算符几乎有无穷的用途。 但是主要思想是,每当不想指定要使用的参数数量时,都可以使用splat运算符。 最简单的示例如下所示:
Another useful thing is that the splat operator can make an array into several arguments:
另一个有用的事情是splat运算符可以将数组变成几个参数:
arr = ["first", "second", "third"]def threeargs(*arr)#makes three arguments
You can also use the splat operator to grab any segment of an array:
您还可以使用splat运算符获取数组的任何段:
first, *rest, last = ["a", "b", "c", "d"]p first # "a"p rest # ["b", "c"]p last # "d"
You’ll notice that the rest variable is still an array, which is super handy. And so, following the last example, you can still do things like this:
您会注意到rest变量仍然是一个数组,非常方便。 因此,按照最后一个示例,您仍然可以执行以下操作:
first, *rest, last = ["a", "b", "c", "d"]p rest[0] # "b"
Those are the basics of the single splat operator, but I urge you to mess around with it more. It can do things like combine arrays, turn hashes and strings into arrays, or pull items out of an array!
这些是单个splat运算符的基础知识,但是我敦促您更多地使用它。 它可以执行诸如合并数组,将哈希和字符串转换为数组或从数组中拉出项之类的操作!
双**啪 (Double **Splat)
The double splat operator came out back in Ruby 2.0. It’s pretty similar to the original splat with one difference: it can be used for hashes! Here’s an example for the most basic use of a double splat.
double splat运算符出现在Ruby 2.0中。 它与原始splat非常相似,但有一个区别:它可用于哈希! 这是最基本的使用双splat的示例。
def doublesplat(**nums) p **numsenddoublesplat one: 1, two: 2 # {:one=>1, :two=>2}
全部放在一起 (Putting it all Together)
I hope you can see that the possibilities are pretty endless with using these two together. The main thing to keep in mind is that you use splats as a parameter in a method when you are unsure of how many arguments that method will be using.
我希望您能看到将两者一起使用的无限可能性。 要记住的主要事情是,在不确定该方法将使用多少个参数时,可以将splats用作方法中的参数。
Lastly, I made a little function that shows how you can filter out any argument that is not a key value pair using both a single splat and double splat.
最后,我做了一个小函数,展示了如何使用单splat和double splat过滤掉不是键值对的任何参数。
def dubSplat(a, *b, **c) p cenddubSplat(1,2,3, 4, a: 40, b: 50)#{:a=>40, :b=>50}
Thanks for reading, and now try playing around with it yourself!
感谢您的阅读,现在尝试自己玩!
翻译自: https://www.freecodecamp.org/news/rubys-splat-and-double-splat-operators-ceb753329a78/
splat net