require 'csv' require_relative 'movies' rows = CSV.read('movies.csv', :headers => true, headers_converter => :symbol, :skip_blanks => true) # Map movies = rows.map { |row| Movie.new(row) } # Select # In this example, get movies which contain a rotten tomatoes score. with_rotten_scores = movies.select { |movie| movie.rotten_tomatoes > 0 } # Reduce average_rotten_score = with_rotten_scores.reduce(0.0) do |total, movie| total + movie.rotten_tomatoes end # Group By # In this example, get movie count released by month movies_by_month = movies.group_by do |movie| movie.release_date.strftime("%B") end # Ruby allows you to break an array in the map arguments count_by_month = movies_by_month.map do |month, list| [month, list.size] end # Sort By # Using Ruby symbol to proc shortcut for last count_by_month_sorted = count_by_month.sort_by(&:last).reverse count_str = count_by_month.map { |pair| pair.join(": ")}.join("\n") # Refactor (clean Up via chaining data methods) count_by_month = movies.group_by do |movie| movie.release_date.strftime("%B") end.map do |month, list| [month, list.size] end.sort_by(&:last).reverse count_str = count_by_month.map { |pair| pair.join(": ")}.join("\n")