Enumerable #group_by

Edward Kim bio photo By Edward Kim

Enumerable #group_by

The enumerable group_by method takes a list and groups it from parameters that the developer sets up. There are a variety of ways that a list can be grouped from the group_by method. I shall go over 2 situations. But first let me create an array and make it equal a dinosaur variable:

dinosaur = ["Tyrranosaurus", "Allosaurus", "Brachiosaurus", "Triceratops", "Ankylosaurus", "Baryonyx"]

1st Situation

Here we are grouping the strings of the array by the first letter of the dinosaur names

As you can see the "T"s, "A"s, and "B"s are all grouped together.
(The comment line shows what the output would be.)

2nd Situation

Here we are grouping the strings of the array by the length of the dinosaur names

As you can see the dinosaur names are grouped based on how many characters are in each name.