开发者

`initialize': undefined method `gauge' (NoMethodError)

开发者 https://www.devze.com 2023-01-15 22:09 出处:网络
Does anyone know why Im getting this error: initialize\': undefined methodgauge\' for # (NoMethodError)?! I mean Im using the attr_accessor for the getters and setters for gauge but still it says that

Does anyone know why Im getting this error: initialize': undefined methodgauge' for # (NoMethodError)?! I mean Im using the attr_accessor for the getters and setters for gauge but still it says that I have not declared it. I have below posted a fragment of the code. Thanks in advance.

class ParkingLotGuest < Guest

attr_accessor :gauge, :electricity_bill, :type_of_stay  

def initialize (name, address, phone, arrival, lives_where, type_of_stay, gues开发者_高级运维t_wishes_electricity)
    super(name, address, phone, arrival, lives_where)
    @type_of_stay = type_of_stay
    @electricity_bill = 0 
    @gauge = 0;
    if (guest_wishes_electricity[0,1] == "j")
        @gauge = lives_where.gauge
    end
end

Here is the code form the parking_lot class:

class Parking_Lot

attr_accessor :nr
    attr_accessor :gauge
attr_reader :electricity_meter

# Initiates a new caravan plot with the number nr and a electricity meter between
# 2000 och 4000.
def initialize (nr)
    @nr = nr
            @gauge = gauge
    @electricity_meter = 4000-rand(2000)
end

# increases the electricity meter for use with a random amount between 
# 10-80 kWh per 24 h.
def increase_meter(days)
    generatedUse = (10+rand(70))*days
    puts "Increases the meter with " + generatedUse.to_s + " kWh."
    @electricity_meter += generatedUse
end

# Returns a string representation of the camping plot.
def to_s

  "Plot #{@nr+1} Electricity meter: #{@electricity_meter} kWh"

end 
end

Code from the camping class.

@staticGuests = Array[   
    ParkingLotGuest.new("Logan Howlett", "Tokyo", "07484822", 1, @parking_lots[0], "Husvagn", "j", @gauge), 
    ParkingLotGuest.new("Scott Summers",    "Chicago", "8908332", 2, @parking_lots[1], "Husbil", "j", @gauge),
  ParkingLotGuest.new("Hank Moody",     "Boston", "908490590",23, @parking_lots[2], "Tält", "n", @gauge),
  CabinGuest.new("Jean Grey", "Detroit", "48058221", 4, @parking_lots[3]['Wolverine']),
  CabinGuest.new("Charles Xavier",  "Washington DC", "019204822", 5, @parking_lots[4]['Thanos'])
]                                                                                                                      


look at the error more carefully:

undefined method `gauge' for #<Parking_Lot:0x10017c860 @electricity_meter=3622, @nr=0> (NoMethodError) 

What this is saying is that an instance of Parking_Lot received a method call for a method named gauge, and it didn't have a gauge method.

In this line, there is only one method call.

@gauge = lives_where.gauge

@gauge = ... assigns directly to the instance variable @gauge, bypassing the setter method, so it's not that.

lives_where.gauge is probably what is complaining. Is lives_where a Parking_Lot?

0

精彩评论

暂无评论...
验证码 换一张
取 消