I am looking for the Ruby-est way to solve a problem.
I have code of a form similar to this in my application:
data = [1,2,3,4]
a = []
b = []
h = {}
data.each do |val|
h[val] = func1(val)
a.push func2(val)
b.push func3(val)
end
This is, of course, a simplification. What I’d like to do, given code like this, is to eliminate lines 2-4 of this snippet, so that I have something like:
h, a, b = data.some_func{|val|
# do something
}
My instinct is that map won’t be enough, but I’m not sure what I need instead. My code works, but it doesn’t look very rubyish. What should I be doing here?
Maybe it’s not exactly what you’re asking for, but at least it’s readable:
Though, as you said, your example is a simplification of a real task, so it might not work for you. In that case I’d recommend to stay with your original solution.