我需要同时发送数组和散列的方法参数。但不知道,怎么样。这是示例:
def method(here should be this argument)
end
def show(*a)
p a
if a.length.even? == true
p Hash[*a]
else
p "hash conversion not possible"
end
end
show(1,2,3,4)
show(1,2,3)
输出:
[1, 2, 3, 4]
{1=>2, 3=>4}
[1, 2, 3]
"hash conversion not possible"
编辑:
来自 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)
输出:
{1=>2, 3=>4}