splat net

Ruby Splat参数 (Ruby Splat Arguments)

We have learnt how to work with methods in Ruby? We are very well aware of the fact that methods may or may not consume any arguments. Let us discuss the methods which consume argument or have a predefined argument list. This argument list may contain any number of arguments but with a compulsion that you will have to pass the same number of arguments with the method as specified in the argument list of the method. But this decreases the versatility of the code, what if you have created a method which adds three numbers but the requirement of the time is to add two numbers only. Your function will fail in this case. One way is to overload the method but the thing is how many times you can overload the method because there may be n number of possibilities and that will eventually make your code redundant. Ruby has a good concept of splat arguments which allows you to pass any number of arguments in the method. Splat arguments are of two types which are discussed in the rest of the article.

我们已经了解了如何在Ruby中使用方法? 我们非常了解以下事实:方法可能会或可能不会使用任何参数。 让我们讨论消耗参数或具有预定义参数列表的方法。 该参数列表可以包含任意数量的参数,但具有强制性,您必须使用方法的参数列表中指定的方法传递相同数量的参数。 但是,这降低了代码的通用性,如果您创建了一个将三个数字相加但方法是仅将两个数字相加的方法,该怎么办。 在这种情况下,您的功能将失败。 一种方法是重载该方法,但问题是您可以重载该方法多少次,因为可能存在n种可能性,最终将使您的代码冗余。 Ruby具有splat参数的良好概念, 它允许您在方法中传递任意数量的参数Splat参数有两种类型,本文其余部分将进行讨论。

1)单个Splat参数 (1) Single Splat Arguments)

Single splat argument is implemented with the help of * operator. You can pass Arrays with the help of them. Refer the syntax given below,

单个splat参数*运算符的帮助下实现。 您可以在它们的帮助下传递数组。 请参考下面给出的语法,

    def method_name( *args)
...
end

The following example will help you to understand its implementation,

以下示例将帮助您了解其实现,

def mul(*args)
pro = 1
args.each do |ar|
pro = pro * ar
end
return pro
end
puts "Product is #{mul(12,44,55,33,22,55)}"
puts "Product is #{mul(1.2,4.4,5.5,3.3,2.2,5.5)}"
puts "Product is #{mul(1,2)}"
puts "Product is #{mul(100,45)}"

Output

输出量

Product is 1159567200
Product is 1159.5672000000002
Product is 2
Product is 4500

You can observe in the above code that we have created a method that is multiplying all the arguments which are passed in it. We are taking help from a splat operator which is allowing us to pass as many arguments we want. We have passed the different number of arguments in each method call. If this is not the case then we might have to define the different methods for the different number of arguments.

您可以在上面的代码中观察到,我们已经创建了一个方法,该方法将传递给它的所有参数相乘。 我们正在从splat运算符获取帮助,该操作符允许我们传递所需的尽可能多的参数。 我们在每个方法调用中传递了不同数量的参数。 如果不是这种情况,那么我们可能必须为不同数量的参数定义不同的方法。

2)Double splat参数 (2) Double splat arguments)

The concept of double splat argument was introduced in Ruby 2.0. The implementation is pretty similar to a single splat argument but an add-on feature that will also work for hashes. It is implemented with the help of ** operator. Following is the syntax that will tell you how can we use double splat arguments.

在Ruby 2.0中引入了double splat参数的概念。 该实现与单个splat参数非常相似,但是其附加功能也适用于哈希。 它在**运算符的帮助下实现。 以下是语法,它将告诉您如何使用双splat参数

    def show( **args)
...
end

Now, let us go through its example for understanding it better.

现在,让我们通过其示例来更好地理解它。

def fruits (**fruits_and_color)
fruits_and_color.each do |frt, color|
puts "Fruits: #{frt}"
puts "Color: #{color}"
end
end
data1 = {"Kiwi": "Green",
"Peach": "Pink",
"Banana": "Yellow",
"Grapes": "Green"
}
data2 = {"Kiwi": "Green",
"Peach": "Pink",
"Banana": "Yellow",
"Grapes": "Green",
"Watermelon": "Blue"
}
fruits data1
fruits data2

Output

输出量

Fruits: Kiwi
Color: Green
Fruits: Peach
Color: Pink
Fruits: Banana
Color: Yellow
Fruits: Grapes
Color: Green
Fruits: Kiwi
Color: Green
Fruits: Peach
Color: Pink
Fruits: Banana
Color: Yellow
Fruits: Grapes
Color: Green
Fruits: Watermelon
Color: Blue
=> {:Kiwi=>"Green", :Peach=>"Pink", :Banana=>"Yellow", :Grapes=>"Green", :Watermelon=>"Blue"

In the above code, you can observe that we have created two hashes data1 and data2. We have created a method fruits and we are passing these hashes with the different number of entries into the method.

在上面的代码中,您可以观察到我们创建了两个哈希data1和data2 。 我们创建了一个方法结果 ,并将这些具有不同条目数的哈希传递给该方法。

翻译自: https://www.includehelp.com/ruby/splat-arguments.aspx

splat net

splat net_Ruby中的Splat参数相关推荐

  1. Struts2中Action接收参数

    Struts2中Action接收参数的方法主要有以下三种: Struts2中Action接收参数的方法主要有以下三种: 1.使用Action的属性接收参数:     a.定义:在Action类中定义属 ...

  2. Struts2中action接收参数的三种方法及ModelDriven跟Preparable接口结合JAVA反射机制的灵活用法...

    Struts2中action接收参数的三种方法及ModelDriven跟Preparable接口结合JAVA反射机制的灵活用法 www.MyException.Cn   发布于:2012-09-15 ...

  3. 如何在Matlab中获取函数参数的数目?

    本图文详细介绍了Matlab中获取函数参数数目的方法.

  4. python参数传递方法_深入理解python中函数传递参数是值传递还是引用传递

    python 的 深入理解python中函数传递参数是值传递还是引用传递 目前网络上大部分博客的结论都是这样的: Python不允许程序员选择采用传值还是传 引用.Python参数传递采用的肯定是&q ...

  5. oracle自动分区maxvalue,分区表中的maxvalue参数设置-Oracle

    分区表中的maxvalue参数设置 结论:partition p3 values less than (maxvalue)   分区表中maxvalue如果用具体参数来代替,则整个表中可插入的最大值不 ...

  6. R语言可视化绘制及PDF使用字体参数列表:查看字体列表、可视化绘制图像中的字体参数列表、字体示例并写入pdf

    R语言可视化绘制及PDF使用字体参数列表:查看字体列表.可视化绘制图像中的字体参数列表.字体示例并写入pdf 目录 R语言可视化绘制及PDF使用字体参数列表:查看字体列表.可视化绘制图像中的字体参数列 ...

  7. seaborn使用violinplot函数可视化小提琴图、并在violinplot函数中设置inner参数来添加数据点显示数据的稠密程度

    seaborn使用violinplot函数可视化小提琴图.并在violinplot函数中设置inner参数来添加数据点显示数据的稠密程度(Seaborn violinplot with data po ...

  8. seaborn使用violinplot函数可视化小提琴图、并在violinplot函数中设置inner参数来添加横线(inner=“stick“)显示数据的稠密程度

    seaborn使用violinplot函数可视化小提琴图.并在violinplot函数中设置inner参数来添加横线(inner="stick")显示数据的稠密程度(Seaborn ...

  9. C#中方法的参数的四种类型(转)

    转自:http://www.cnblogs.com/netlyf/p/3822956.html C#中方法的参数有四种类型: 1. 值参数类型  (不加任何修饰符,是默认的类型) 2. 引用型参数   ...

最新文章

  1. Python入门篇-匿名函数
  2. fileZilla连接oracle服务器,传DMP文件
  3. 暗网 tor溯源困难根因——用户的请求会在分布全球的主机随机跳转三次,最终才到达服务器,这就造成了溯源的极其困难...
  4. Java知识点总结(Java容器-EnumSet)
  5. 控制iOS的导航栏和状态栏的样式
  6. 前沿分享|阿里云数据库高级技术专家 宋利兵:阿里云企业级自治数据库RDS详解
  7. 进程与线程的超级简单形象解释
  8. Eclipse Collections随Java版本的演变
  9. oracle sql 分页
  10. Serilog 自定义 Enricher 来增加记录的信息
  11. AndroidStudio_使用NanoHTTPD搭建HTTP服务_把android设置当成一个http服务器来使用---Android原生开发工作笔记225
  12. 计算机四级网络工程师考点速查,计算机四级《网络工程师》考点习题
  13. 单片机程序如何反编译成C语言,如何实现单片机程序代码的反汇编
  14. 移动接入身份认证技术
  15. 利用js,HTML,css实现一个简单的指针时钟
  16. 虚无鸿蒙混沌系统,玄幻 鸿蒙混沌选择系统
  17. HR不排斥的三大跳槽理由
  18. matlab归一程序,Matlab三种归一化方法
  19. Arranging The Sheep 中位数定理
  20. hadoop 任务运行到running job就卡住了 INFO mapreduce.Job: Running job: job_XXXXXXX

热门文章

  1. codeforces C. Design Tutorial: Make It Nondeterministic
  2. php 分割二维数组,拆分二维数组 php
  3. 清理offset_关于 kafka 日志清理策略的问题
  4. js 创建keyframe_javascript – 查找特定的CSS @keyframes规则
  5. mysql新增阵列df_DF学Mysql(三)——索引操作
  6. java io中断_JDK源码阅读:InterruptibleChannel 与可中断 IO
  7. 的setinterval函数_ES6 极简教程 lt;6gt; 函数扩展
  8. Portainer简介及部署
  9. 第一章、第一节 Angular基础
  10. Transport Ship【多重背包】