Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 如何同时生成方法、数组和哈希的参数_Ruby - Fatal编程技术网

Ruby 如何同时生成方法、数组和哈希的参数

Ruby 如何同时生成方法、数组和哈希的参数,ruby,Ruby,我需要同时发送数组和哈希的方法参数。但不知道,怎么做。下面是一个例子: def method(here should be this argument) end 输出: 编辑: 根据OP的注释,这里是OP可以使用的代码: def add(*arg) @entries = {} arg.each_slice(2) do |a| @entries[a.first] = a.last end p @entries end add(1,2,3,4) 输出: 你所说的同时是什

我需要同时发送数组和哈希的方法参数。但不知道,怎么做。下面是一个例子:

def method(here should be this argument)
end
输出:

编辑:

根据OP的注释,这里是OP可以使用的代码:

 def add(*arg)
   @entries = {}
   arg.each_slice(2) do |a| @entries[a.first] = a.last end
   p @entries
 end

 add(1,2,3,4)
输出:


你所说的同时是什么意思?对象可以是任何一种类型?您好。我对这个参数使用“each”并得到错误。如何仅对数组使用“每个”?如何检查参数?这是我的代码:def add(defs)defs.each do | word,definition |@entries[word]=definition endend@JohnOggy
每个
只能接受一个参数。
[1, 2, 3, 4]
{1=>2, 3=>4}

[1, 2, 3]
"hash conversion not possible"
 def add(*arg)
   @entries = {}
   arg.each_slice(2) do |a| @entries[a.first] = a.last end
   p @entries
 end

 add(1,2,3,4)
{1=>2, 3=>4}