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
class Memo | |
def initialize | |
@mutex = Mutex.new | |
@values = {} | |
end | |
def memoize(key = :_default_key) | |
raise ArgumentError, "Block is required" unless block_given? | |
@mutex.synchronize do |
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
# frozen_string_literal: true | |
# Represents an immutable Result object that can contain either a successful value | |
# or an error message. This implements the Result pattern for handling success/failure | |
# scenarios in a functional way. | |
Result = Data.define(:value, :error_message, :creation_location) do | |
def self.ok(value) | |
new(value: value, error_message: nil, creation_location: caller_locations(1, 1).first) | |
end |
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 |