How to do recursion?

For example, there are categories and nested subcategories.
Here is my code recursion (Ruby):
def recurs(category_id)
 @list << category_id
 Category.where(parent_id: category_id).each do |category|
recurs(category.id)
end
 end

And here's cause:
@list = []
recurs(7)

In the end, the @list is what I need.
But the challenge does not look normal.
How to do recursion, that even if it were possible so to touch id-shnik:
@list = recurs(7)
???
June 8th 19 at 16:28
2 answers
June 8th 19 at 16:30
Solution
Late, but...
def recurs(category_id, list=[])
 list << category_id
 Category.where(parent_id: category_id).each do |category|
 recurs(category.id, list)
end
list
end
recurs 7
June 8th 19 at 16:32
Humane way
def recurs(category_id)
 list << category_id
 Category.where(parent_id: category_id).each do |category|
recurs(category.id)
end
list
 end


@list = recurs(7)
Well, not working well is abusing the lack of list - Peggie_Sawayn73 commented on June 8th 19 at 16:35
most likely the task is set incorrectly. It will be useful to write in any applied problem that must be solved. - ophelia_Kshlerin commented on June 8th 19 at 16:38
The issue fully. It is necessary to receive the data via the method and not, as described in the method. - Peggie_Sawayn73 commented on June 8th 19 at 16:41

Find more questions by tags Ruby