Ruby: NoMethodError: undefined method when Mutating Instance Variable
Ruby: NoMethodError: undefined method when Mutating Instance Variable
我正在编写一个程序,可以重新安排公共汽车上的乘客。我想记录重新排序公共汽车座位需要多少次洗牌。以为我有,但我无法改变self.reshuffle_count
。如何从方法 reshuffle_seating
访问 @reshufffle_count
class MagicBus < Array
attr_writer :seating
def seating
@seating || []
end
def reshuffle_count
@reshuffle_count || 0
end
def reshuffle_seating
max_passengers = self.seating.max
popular_seat = self.seating.max
pop_seat_indx = self.seating.find_index(popular_seat)
reshuffle_num = 0
self.seating.rotate!(pop_seat_indx)
while popular_seat > 1 do
self.seating.each_with_index do |seat, index|
if index != 0
self.seating[index] = self.seating[index] +1
popular_seat = popular_seat -1
else
reshuffle_num = reshuffle_num +1
p reshuffle_num
end
end
end
self.seating.rotate!(pop_seat_indx*-1)
self.seating[pop_seat_indx] = popular_seat
self.reshuffle_count = reshuffle_num
end
end
class TestMagicBusSeating < MiniTest::Test
def test_reshuffle_seating_instance1
bus = MagicBus.new
bus.seating = [0,2,7,0]
bus.reshuffle_seating
assert_equal([2, 4, 1, 2], bus.seating)
assert_equal(5, bus.reshuffle_count)
end
end
错误信息是
Error:
TestMagicBusSeating#test_reshuffle_seating_instance1:
NoMethodError: undefined method `reshuffle_count=' for []:MagicBus
Did you mean? reshuffle_count
magic_bus.rb:99:in `reshuffle_seating'
magic_bus.rb:217:in `test_reshuffle_seating_instance1'
NoMethodError: undefined method `reshuffle_count='
这意味着您缺少 setter
。您定义了 getter def reshuffle_count
但没有 setter.
class MagicBus < Array
attr_writer :seating
def seating
@seating || []
end
def reshuffle_count=(value)
@reshuffle_count=value
end
def reshuffle_count
@reshuffle_count || 0
end
.....
或者正如@tadman 指出的那样,attr_writer :resuffle_count
除了接受的答案外,您还可以直接访问相同class的其他方法内部的实例变量。
class MagicBus < Array
def reshuffle_count
@reshuffle_count || 0
end
def reshuffle_seating
...
@reshuffle_count = reshuffle_num
end
end
请注意,如果您定义,
attr_writer :resuffle_count
那么 MagicBus 实例也可以 read/write class.
之外的变量
magic_bus = MagicBus.new
magic_bus.resuffle_count # valid
除非你把 attr_writer :resuffle_count
放在私有范围内。
class MagicBus < Array
...
def reshuffle_seating
...
reshuffle_count = reshuffle_num
end
private
attr_writer :resuffle_count
end
现在resuffle_count
只能在class内访问。
我正在编写一个程序,可以重新安排公共汽车上的乘客。我想记录重新排序公共汽车座位需要多少次洗牌。以为我有,但我无法改变self.reshuffle_count
。如何从方法 reshuffle_seating
@reshufffle_count
class MagicBus < Array
attr_writer :seating
def seating
@seating || []
end
def reshuffle_count
@reshuffle_count || 0
end
def reshuffle_seating
max_passengers = self.seating.max
popular_seat = self.seating.max
pop_seat_indx = self.seating.find_index(popular_seat)
reshuffle_num = 0
self.seating.rotate!(pop_seat_indx)
while popular_seat > 1 do
self.seating.each_with_index do |seat, index|
if index != 0
self.seating[index] = self.seating[index] +1
popular_seat = popular_seat -1
else
reshuffle_num = reshuffle_num +1
p reshuffle_num
end
end
end
self.seating.rotate!(pop_seat_indx*-1)
self.seating[pop_seat_indx] = popular_seat
self.reshuffle_count = reshuffle_num
end
end
class TestMagicBusSeating < MiniTest::Test
def test_reshuffle_seating_instance1
bus = MagicBus.new
bus.seating = [0,2,7,0]
bus.reshuffle_seating
assert_equal([2, 4, 1, 2], bus.seating)
assert_equal(5, bus.reshuffle_count)
end
end
错误信息是
Error:
TestMagicBusSeating#test_reshuffle_seating_instance1:
NoMethodError: undefined method `reshuffle_count=' for []:MagicBus
Did you mean? reshuffle_count
magic_bus.rb:99:in `reshuffle_seating'
magic_bus.rb:217:in `test_reshuffle_seating_instance1'
NoMethodError: undefined method `reshuffle_count='
这意味着您缺少 setter
。您定义了 getter def reshuffle_count
但没有 setter.
class MagicBus < Array
attr_writer :seating
def seating
@seating || []
end
def reshuffle_count=(value)
@reshuffle_count=value
end
def reshuffle_count
@reshuffle_count || 0
end
.....
或者正如@tadman 指出的那样,attr_writer :resuffle_count
除了接受的答案外,您还可以直接访问相同class的其他方法内部的实例变量。
class MagicBus < Array
def reshuffle_count
@reshuffle_count || 0
end
def reshuffle_seating
...
@reshuffle_count = reshuffle_num
end
end
请注意,如果您定义,
attr_writer :resuffle_count
那么 MagicBus 实例也可以 read/write class.
magic_bus = MagicBus.new
magic_bus.resuffle_count # valid
除非你把 attr_writer :resuffle_count
放在私有范围内。
class MagicBus < Array
...
def reshuffle_seating
...
reshuffle_count = reshuffle_num
end
private
attr_writer :resuffle_count
end
现在resuffle_count
只能在class内访问。