Implementing a Potluck Dinner System with Backward Compatibility in Ruby

Introduction

Welcome to today's lesson, where we will tackle a common challenge in software engineering: the introduction of complex features while preserving backward compatibility. Our focus will be on a Potluck Dinner organization system, where we will manage participants and their respective dishes for each round. Get ready for an exciting journey through Ruby programming, step-by-step analysis, and strategic thinking. Let's dive into our adventure!

Starter Task Review

Initially, our Potluck Dinner organization system allows us to add and remove participants and manage their respective dishes for each round. There are three essential methods:

  • add_participant(member_id): This method adds a participant. If a participant with the given member_id already exists, it won't create a new one but will return false. Otherwise, it will add the member and return true.
  • remove_participant(member_id): This method removes a participant with the given member_id. If the participant exists, the system will remove them and return true. Otherwise, it will return false. When removing a participant, you need to remove their dish if they brought one.
  • add_dish(member_id, dish_name): This method enables each participant to add their dishes for every round. If a participant has already added a dish for this round OR if the member_id isn't valid, the method will return false. Otherwise, it will add the dish for the respective participant's round and return true.

Let's write our Ruby code, which implements the functions as per our initial state:

Ruby
class Potluck
  def initialize
    @participants = {}
    @dishes = {}
  end

  def add_participant(member_id)
    return false if @participants.key?(member_id)

    @participants[member_id] = true
    true
  end

  def remove_participant(member_id)
    return false unless @participants.key?(member_id)

    @participants.delete(member_id)
    @dishes.delete(member_id)
    true
  end

  def add_dish(member_id, dish_name)
    return false unless @participants.key?(member_id)
    return false if @dishes.key?(member_id)

    @dishes[member_id] = dish_name
    true
  end
end

In this code, we employed Ruby hashes to store unique participant IDs and their respective dish names. With this foundation laid, let's introduce some advanced functionalities.

Introducing Advanced Functionalities

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal