'Trying to create a method in Ruby to return all possible combinations of an array using recursion (not built in methods like combination/permutation) [closed]

I'm learning Ruby and would like to create a method that returns all possible combinations of an array using recursion without using built in methods like combination or permutation. Specifically I would like to convert the following Javascript code into something similar for Ruby:

const combinations = (elements) => {
  if (elements.length === 0) return [ [] ];
  const firstEl = elements[0]
  const rest = elements.slice(1);

  const combsWithoutFirst = combinations(rest);
  const combsWithFirst = [];

  combsWithoutFirst.forEach(comb => {
    const combWithFirst = [...comb, firstEl];
    combsWithFirst.push(combWithFirst);
  });

  return [ ...combsWithoutFirst, ...combsWithFirst ];
}

I'm quite new to programming and I'm still trying to get my head around recursion, any help would be appreciated!

The code above comes from this youtube video: https://www.youtube.com/watch?v=NA2Oj9xqaZQ



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source