Created
June 21, 2024 01:06
-
-
Save hmaddocks/a126dedb44c72f308912a1f6b8790ff9 to your computer and use it in GitHub Desktop.
Refactor Code Found on LinkedIn
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Case 2: Refactor code | |
class ProjectsCount | |
def initialize(projects) | |
@projects = projects | |
end | |
def self.call(projects) | |
new(projects).call | |
end | |
def call | |
nda_projects = [] | |
not_nda = [] | |
projects.map do |project| | |
nda_projects << project if project.name == 'NDA-Project' | |
not_nda << project if project.name != 'NDA-Project' | |
end | |
count(not_nda, nda_projects) | |
end | |
private | |
attr_reader :projects | |
def count(not_nda, nda_projects) | |
count = (not_nda.uniq + nda_projects).count | |
"#{count} project" | |
end | |
end | |
Project = Struct.new(:name) | |
projects = [Project.new("NDA-Project"), Project.new("NDA-Project"), Project.new("Foo-Project"), Project.new("NDA-Project"), Project.new("Foo-Project")] | |
counter = ProjectsCount.call(projects) | |
p counter | |
def projects_count(projects) | |
nda, not_nda = projects.partition { |project| project.name == 'NDA-Project' } | |
"#{nda.count + not_nda.uniq.count} project".pluralize | |
end | |
p projects_count(projects) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment