Recap: Limited Store
Lesson 42 of 48 in Coddy's Ruby for Lua Developers course.
Challenge
EasyDefine Store(capacity), with put(key,value), get(key), and size. put returns false for a new key when full, but updates existing keys even when full. get returns nil if absent. solve input starts with capacity then key=value pairs; return size|sorted key:value pairs.
Return text from solve(input) without printing. The locked driver prints the returned text with one final newline and checks required public APIs independently.
Your code goes in exercise.rb, store.rb. main.rb holds the supplied input/output code and cannot be edited.
Try it yourself
require_relative 'exercise'
require_relative 'store'
def check(value)
raise 'Public API check failed' unless value
end
s = Store.new(1)
check(s.get("missing").nil? && s.put("a", 0) && !s.put("b", 2))
check(s.put("a", 3) && s.get("a") == 3 && s.size == 1 && s.get("b").nil?)
puts solve(STDIN.read)
All lessons in Ruby for Lua Developers
1Start with Ruby
Run Ruby FilesOutput and CommentsNames and ObjectsInteger ArithmeticTruth and Boolean LogicString OperationsSymbols and KeysRecap: Packing Slip4Mutation and Errors
Mutation and AliasesNil and Optional ValuesExceptions and ValidationEnsure and CleanupRecap: Safe Ratio7Objects in Practice
Ordering with ComparableCopies and Frozen ValuesLambdas and Captured StateCustom EnumerableRecap: Callable FilterRecap: Limited Store2Flow and Methods
Conditional ValuesCase and RangesLoops and Early ExitMethods and ReturnsDefaults and KeywordsRecap: Sensor Run5Objects and State
Modules as NamespacesClasses and InstancesReaders and WritersValidated ConstructionRecap: Wallet8Lending Desk Project
Lending Desk: CatalogLending Desk: BorrowPractice on your own: Online Ruby compiler