ruby - Ruby 中的 3 点方法参数是什么?
问题描述
谁能解释一下这个语法:
def hello(...)
p(...).to_a
end
hello 1,2,3,4 # => [1,2,3,4]
什么是类型...
?
解决方案
我认为这篇文章将帮助您更好地理解它。简而言之,这是一种新的“速记语法”,用于引导参数以使代码“更简单”,现在call(*args, **kws, &block)
您可以编写call(...)
这是一个简单的例子:
def perform(*args, **kws, &block)
block.call(args, kws)
end
def call(...)
perform(...)
end
> call(1, 2, 3, k1: 4, k2: 5) {|*x| puts x}
1
2
3
{:k1=>4, :k2=>5}
推荐阅读
- javascript - 上传到服务器后网站出现问题
- linux - 使用 shell 脚本验证设备配置
- java - 如何使用 Java 将“MongoSocketException”修复到外部 MongoDB?
- css - 为第一列设置替代行颜色
- python - RuntimeError: 断言 `cur_target >= 0 && cur_target < n_classes' 失败
- c# - c#如何在放大地图中将经纬度转换为xy平面
- ruby-on-rails - 从第一个工作人员调用第二个工作人员时如何在第二个工作人员中获取工作 ID
- file - 如何从 Android Studio 查看设备文件 - 文件资源管理器不允许访问
- php - 如何为自定义 symfony 引导程序设置环境
- xamarin - Xamarin.Forms Ios 图像