Object-Oriented Design Practice Guide
We are learning from Ruby's evolving flexible applications.
CHAPTER VIII COMBINING OBJECTS IN COMPOSITION
8.4 Actual
Cycle sample source configured
When I moved it on my local terminal, the following error occurred.
The arguments for PartsFactory.build are config, parts_class=Parts
There are two things that exist, so it doesn't move in the first place, does it?
I am reviewing books and searching by error code.
Not resolved
I would appreciate it if you could help me.Thank you for your cooperation.
Traceback (most recent call last):
1:from cycle.rb:65:in`<main>'
cycle.rb:32:in `build':uninitialized constant PartsFactory::Parts(NameError)
class Bicycle
attr_reader:size,:parts
def initialize(args={})
@size=args [:size]
@ parts = args [: parts ]
end
def spares
parts.spares
end
end
require 'forwardable'
class part
extend Forwardable
def_delegators:@parts, :size, :each
include Enumerable
def initialize (parts)
@ parts = parts
end
def spares
select { | part | part.needs_spare }
end
end
require 'ostruct'
module PartsFactory
def self.build (config, parts_class=Parts)
parts_class.new(
config.collect { | part_config |
create_part(part_config)
}
)
end
def self.create_part(part_config)
OpenStruct.new(
name —part_config [0],
description:part_config[1],
needs_spare —part_config.fetch(2, true)
)
end
end
road_config = [
['chain', '10-speed'],
['tire_size', '23'],
['tape_color', 'red']
]
mountain_config = [
['chain', '10-speed'],
['tire_size', '2.1',]
['front_shock', 'Manitou', false',
['real_sshock', 'Fox']
]
road_bike=
Bicycle.new(
size: 'L',
parts —PartsFactory.build (road_config)
)
About
Ruby version: ruby 2.6.5p114
It was solved with the advice of popopo!
The error is that the parts class could not be found.The class name declared in line 15 is Part, so I think it'll be fine if you change it to Parts – popopo
© 2024 OneMinuteCode. All rights reserved.