Hope I understood your problem right:
method(h[0].values_at("value"),
h[1] ? h[1].values_at("value") : []
)
Your problem: if h[1]does not exist, h[1].values_at will raise an exception. So you have to test first, if the value is available. In the code snipllet above I used a ternary operator.
An extended version would be:
par2 = []
par2 = h[1].values_at("value") if h[1]
method(h[0].values_at("value"),
par2
)
With my solution you don't need the default values in the method definition.
In your comment you extended your question.
With four parameters you could use it like this:
def method(p1,p2,p3,p4)
#...
end
method(
h[0] ? h[0].values_at("value") : [],
h[1] ? h[1].values_at("value") : [],
h[2] ? h[2].values_at("value") : [],
h[3] ? h[3].values_at("value") : [],
)
But I would recommend a more generic version:
def method(*pars)
#pars is an Array with all values (including empty arrays.
#The next check make only sense, if you need exactly 4 values.
raise "Expected 4 values, get #{pars.size}" unless pars.size == 4
end
method( *(h.map{|i|i.values_at("x")})
And another - probably not so good - idea:
Extend nil (the result of h[1] if h has not this element) to return [] for values_at:
class NilClass
def values_at(x)
[]
end
end