You can combine arrays and iterate over the result:
(@users + @users1 + @users2).each do |user| ... end
It may be tempting to use a flattened one, but you should be careful with it. A simple call to no-arguments flatten does not behave as you would expect if any elements of any array are themselves arrays:
users, users1, users2 = [1,2], [ [3,4], [5,6] ], [ 7,8] puts [users,users1,users2].flatten.inspect
However, as jleedev suggests in one of the comments, a flatten function in Ruby can take an integer argument, which determines the maximum level the arrays will be broken, so:
puts [users,users1,users2].flatten(1).inspect # Will print [1, 2, [3, 4], [5, 6], 7, 8] # Just as planned.
source share