运行此代码时出错。以下是输出:
L
Bicycle#Ex3.rb:32:in `spares': private method `select' called for nil:NilClass (NoMethodError)
from Bicycle#Ex3.rb:10:in `spares'
from Bicycle#Ex3.rb:111:in `<main>'
以下是代码:
class Bicycle
attr_reader :size, :parts
def initialize(args={})
@size = args[:size]
@parts = args[:parts]
end
def spares
parts.spares # return an array
end
def lead_days
1
end
#...
end
class Parts
attr_reader :parts
def initialize(args={})
@parts = parts
end
def size
parts.size
end
def spares
parts.select{|part| part.needs_spare}
end
end
class Part
attr_reader :name, :description, :needs_spare
def initialize(args)
@name = args[:name]
@description = args[:description]
@needs_spare = args.fetch(:needs_spare, true)
end
end
class RoadBikeParts < Parts
attr_reader :tape_color
def post_initialize(args)
@tape_color = args[:tape_color]
end
def local_spares
{tape_color: tape_color}
end
def default_tire_size
'23'
end
end
class MountainBikeParts < Parts
attr_reader :front_shock, :rear_shock
def post_initialize(args)
@front_shock = args[:front_shock]
@rear_shock = args[:rear_shock]
end
def local_spares
{ rear_shock: rear_shock}
end
def default_tire_size
'2.1'
end
end
chain = Part.new(
name: 'chain',
description: '10 speed')
road_tire = Part.new(
name: 'tape_size',
description: '23')
tape = Part.new(
name: 'tape_color',
description: 'red')
mountain_tire = Part.new(
name: 'tire_size',
description: '2.1')
rear_shock = Part.new(
name: 'rear_shock',
description: 'Fox')
front_shock = Part.new(
name: 'front_shock',
description: 'Manitou',
needs_spare: false)
road_bike_part = Parts.new([chain, road_tire, tape])
road_bike = Bicycle.new(
size: 'L',
parts: Parts.new([chain,
road_tire,
tape]))
puts road_bike.size
#puts road_bike.parts.size
puts road_bike.spares.size
很明显这一行 - &gt; put road_bike.spares.size给出了错误NoMethodError,但是,我不知道如何为此示例解决此问题。 spares方法返回一个Part对象数组,但似乎我的问题在于spares方法.select是来自调用对象的私有。
任何修改此代码的建议都会很棒。谢谢。
答案 0 :(得分:2)
这里发生的事情是Parts#parts
是nil
。你在这一行得到错误:
# parts is nil
parts.select{|part| part.needs_spare}
在Parts
的初始值设定项中,其parts
属性未正确分配:
def initialize(args={})
@parts = parts
end
因此,在初始化时,它会为@parts
分配值parts
。但由于parts
不是局部变量,因此调用Parts#parts
方法,返回nil
。
如果您将初始化程序更改为以下内容:
def initialize(parts)
@parts = parts
end
您将能够运行代码。但是Parts
的子类似乎期望初始化器中有Hash
,而不是像他们的超类那样Array
。