March 17 - Build a File Type Hash Mapper in Ruby
Whether it's a complex coding interview question or algorithm implementation, there are many times where you will be asked to combine a large number of processes into a single feature. In this guide we'll examine how to build a non-trivial program that takes in an array of file names and generates a hash collection where each file type is the key and the value is an array of their respective file names.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
  • Complete the Exercise
Video locked
This video is viewable to users with a Bottega Bootcamp license

Summary

Build a method that takes in an array of file names and filters them into an organized hash.

Exercise Description

Given an array of file names (with file extensions), build a method that splits the array into a hash where the keys are the file types and the values are arrays that contain the file name.

Example

file_names = %w{file1.rb file2.html file3.rb file4.rb file5.js}

# Result should be:
{
  'rb' => ['file1', 'file3', 'file4'],
  'html' => ['file2'],
  'js' => ['file5']
}

Real World Usage

This is a non-trivial exercise. In order to properly implement this solution you will need to utilize the following valuable skills:

  • Opening up the Array class and adding a custom method
  • Iterating over an array and dynamically building a hash with default empty arrays for each of the values
  • Working with custom File methods
  • Implement string parsing methods to clean up output

Test Cases

Code File