splat net_Ruby的* Splat和double ** Splat运算符简介
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
splat net_Ruby的* Splat和double ** Splat运算符简介相关推荐
- splat net_Ruby中的Splat参数
splat net Ruby Splat参数 (Ruby Splat Arguments) We have learnt how to work with methods in Ruby? We ar ...
- 算术运算符举例java_Java的算术运算符简介
Java的算术运算符简介 算术运算符号,就是用来处理四则运算的符号,这是最简单,也最常用的符号,尤其是数字的处理,几乎都会使用到算术运算符号.下面小编为大家整理了关于Java的算术运算符简介,一起来看 ...
- 数字货币 区块链 双花攻击 Double Spend Attack 简介
前段时间,比特黄金BTG遭受双花攻击,一名恶意矿工临时控制了BTG区块链,在向交易所充值后迅速提币,再逆转区块,成功实施双花攻击. 比特黄金BTG创始人廖翔回应说:"已与各交易所紧密合作,通 ...
- C#中的问号运算符简介
C#中的问号运算符分为三种功能: 第一种:单问号作为条件判断的三元运算符. 这种方式可以处理一些简单的IF结构的条件语句, 比如简单的根据条件结果赋值的功能: int a = 4; int b = - ...
- detail texture与splat texture
detail texture 如果单纯地使用一张texture,在摄像机距离很近的时候,会因为放大的原因导致走样严重,这时可以叠加一个detail texture,该texture使用一定数量的til ...
- C++中的运算符重载基础
1.C++中的运算符重载基础 所谓重载,就是赋予新的含义.函数重载(Function Overloading)可以让一个函数名有多种功能,在不同情况下进行不同的操作.运算符重载(Operator Ov ...
- PHP相等(==双重等于)和标识(===三次等于)比较运算符有何区别?
==和===什么区别? 松散==比较到底如何工作? 严格===比较如何工作? 有什么有用的例子吗? #1楼 这都是关于数据类型的. 以BOOL (对或错)为例: true也等于1 , false也等于 ...
- Day04 关键字、标识符、变量及运算符
关键字 标识符注意点 整数拓展 二进制以0b开头,比如:0b10: 八进制以0开头,比如:010: 十六进制以0x开头,比如:0x10. 浮点数扩展 涉及银行业务计算钱的时候,不用float或者dou ...
- c++运算符重载+的三种类型
小编在练习的过程中发现,C++<运算符重载+>有三种情况: 同一个类:对象A与对象B的加法运算,加法结果赋值给对象A,并没有C=A+B; cout<<C;而是A+B;cout& ...
最新文章
- 你不知道的javaScript笔记(5)
- 一种PacBio测序数据组装得到的基因组序列的纠错方法
- Web网站的性能测试工具
- Python69个内置函数分类总结
- art-template-loader:template
- spring配置详解-复杂类型注入
- 通过Rancher Desktop在桌面上运行K8s
- Liferay7 BPM门户开发之4: Activiti事件处理和监听Event handlers
- 如何正确设置同时使用内外网
- MAC m1 PRO 安装安卓手机模拟器
- win8桌面计算机图标不见,win8桌面图标消失,win8桌面图标设置方法
- 科技论文计算机排版格式,科技论文排版参考格式
- 用matlab产生chu序列和frank序列
- 名悦集团:车上不能缺的行车小物件,安全第一条
- Matlab 元胞自动机(模拟传染病传播)
- Metabase新一代自助数据探索型开源BI
- 梅林安装opkg后安装iperf3_路由器最高速度/性能测试 - Windows 安装 IPerf3 及 使用方法...
- CentOS8 解决SSH Secure Shell 报错 Algorithm negotiation failes
- 汇编语言——跳转指令: JMP、JECXZ、JA、JB、JG、JL、JE、JZ、JS、JC、JO、JP
- 王道计算机网络第三章