2013-05-26 44 views
2

Noob的紅寶石,並在我自己的版本的智能恆溫器演習工作。我想設置一個初始化的目標溫度參數,然後使用方法update_temperature中的當前溫度參數!出於某種原因,它拋出了一個有關未定義局部變量的錯誤。智能家居溫控器

任何想法?

class House 
    def initialize(target_temp, max_temp, min_temp) 
    @target_temp = target_temp 
    @max_temp  = max_temp 
    @min_temp  = min_temp 
    end 

    def update_temperature!(current_temp) 
    @current_temp = current_temp 
    if @min_temp < @current_temp && @current_temp < @target_temp 
     degrees_ac_off = @target_temp - @current_temp 
     puts "The current temperature is #{degrees_ac_off} below the target temperature of #{target_temp}. The air conditioner is off." 
    elsif @current_temp < @min_temp 
     degrees_below = @min_temp - @current_temp 
     puts "The current temperature is #{degrees_below} the minimum desired temperature of #{min_temp}. The heater is on." 
    elsif @target_temp < @current_temp && @current_temp < @max_temp 
     degrees_ac = @current_temp - @target_temp 
     puts "The current temperature is #{degrees_high} more than the target temperature of #{target_temp}. The air conditioner is on low." 
    else @max_temp < @current_temp 
    degrees_ac = @current_temp - @target_temp 
    puts "The current temperature is #{degrees_ac} more than the target temperature of #{target_temp}. The air conditioner is on high." 
    end 
    end 
end 


my_house = House.new(71,75,60) 
my_house.update_temperature!(80) 
+0

什麼是確切的錯誤,它發生在哪裏? – Kai

+0

'else @max_temp <@ current_temp'並不是很多用處。 – Johnsyweb

+0

此代碼(已刪除NameErrors)在達到目標溫度時將airco切換爲高電平。 – steenslag

回答

-1

你錯過了@標誌#{target_temp}#{min_temp}如下。

  • puts "The current temperature is #{degrees_ac_off} below the target temperature of #{target_temp}. The air conditioner is off."

  • puts "The current temperature is #{degrees_below} the minimum desired temperature of #{min_temp}. The heater is on."

  • puts "The current temperature is #{degrees_high} more than the target temperature of #{target_temp}. The air conditioner is on low."

  • puts "The current temperature is #{degrees_ac} more than the target temperature of #{target_temp}. The air conditioner is on high."

使用下面的代碼:

class House 
    def initialize(target_temp, max_temp, min_temp) 
    @target_temp = target_temp 
    @max_temp  = max_temp 
    @min_temp  = min_temp 
    end 

    def update_temperature!(current_temp) 
    @current_temp = current_temp 
    if @min_temp < @current_temp && @current_temp < @target_temp 
     degrees_ac_off = @target_temp - @current_temp 
     puts "The current temperature is #{degrees_ac_off} below the target temperature of #{@target_temp}. The air conditioner is off." 
    elsif @current_temp < @min_temp 
     degrees_below = @min_temp - @current_temp 
     puts "The current temperature is #{degrees_below} the minimum desired temperature of #{@min_temp}. The heater is on." 
    elsif @target_temp < @current_temp && @current_temp < @max_temp 
     degrees_high = @current_temp - @target_temp 
     puts "The current temperature is #{degrees_high} more than the target temperature of #{@target_temp}. The air conditioner is on low." 
    else @max_temp < @current_temp 
    degrees_ac = @current_temp - @target_temp 
    puts "The current temperature is #{degrees_ac} more than the target temperature of #{@target_temp}. The air conditioner is on high." 
    end 
    end 
end 


my_house = House.new(71,75,60) 
my_house.update_temperature!(80) 
#=> The current temperature is 9 more than the target temperature of 71. The air conditioner is on high. 
+0

'degrees_high'是等待發生的另一個NameError。 – steenslag

+0

@steenslag更正!我沒有看到,因爲op的代碼在我的文章中爲上述提到的代碼行拋出了錯誤。但是,感謝指針。 –

+0

請不要要求人們投票/接受你的答案。 [詳細信息請參閱此元帖子](http://meta.stackexchange.com/q/167155/152134)。謝謝! –

1

您正在使用#{target_temp}它調用一個方法,你可以把@符號之前或添加訪問方法他們。我更喜歡後者。

class House 
    attr_accessor :target_temp, :min_temp, :max_temp