0

Ruby 初学者并建立一个 House 类来读取恒温器温度。我在正确的轨道上,我将如何调用更新温度的方法?

让我们编写一些代码来模拟房屋及其恒温器的行为。对于这个挑战,您应该定义一个 House 类。这是您应该建模的行为:

每个房子都有自己的当前温度每个房子都有一个名为 update_temperature 的方法!这将根据加热器或空调是否打开而升高或降低温度。它还将当前温度打印到屏幕上。开暖气时,当前温度升高 1 个单位 开空调时,当前温度降低 2 个单位

您可以打开/关闭加热器,也可以打开/关闭空调

作为奖励,您可以为智能房屋添加此行为:

每个房子都有自己的最低和最高温度当 current_temperature 达到最高温度时,空调打开并且加热器关闭(如果它是打开的)当 current_temperature 达到最低温度时,加热器打开并且空调关闭(如果它打开)每个房子都应该有自己的状态,这意味着我可以创建 House 类的许多不同实例,每个实例都有自己的温度。

class House
  def initialize(current_temp, heater, air_cond)
    @current_temp = current_temp
    @heater = heater
    @air_cond = air_cond
  end

  def update_temperature!
    if @heater
      puts @current_temp += 1
    elsif @air_cond
      puts @current_temp -= 2
    end
  end
end

my_house = House.new(34, false, true)
my_house.update_temperature!
4

2 回答 2

3

您正在这里运行模拟。首先为物理单位安装 SY gem:

gem install sy

然后,这就是如何绕过受控环境模拟:

require 'sy' # physical units
module ControlledEnvironment
  class Air
    SPECIFIC_HEAT_CAPACITY = 1.005.kJ.kg(-1).K(-1)
    attr_accessor :volume, :temperature, :humidity, :pressure, :gas_composition
    # to simplify things, let's forget about humidity, gas compositions...

    def initialize( volume: 500.m(3),
                    temperature: SY::TRIPLE_POINT_OF_WATER + 20.K,
                    pressure: 101.3.kPa )
      @volume, @temperature, @pressure = volume, temperature, pressure
    end

    def density
      density_of_air_at_0_celsius = 1.205.kg.m(-3)
      atmospheric_pressure = 101.3.kPa
      density_of_air_at_0_celsius *
        SY::TRIPLE_POINT_OF_WATER / temperature *
        pressure / atmospheric_pressure
    end

    def heat!( energy )
      @temperature += energy / ( SPECIFIC_HEAT_CAPACITY * volume * density )
    end

    def cool!( energy ); heat -energy end
  end

  class ThermalExchanger
    attr_accessor :power_output
    attr_reader :target

    def initialize( power_output, target )
      @power_output = power_output
      @target = target
    end

    def act( delta_time, pow=power_output )
      target.heat! pow * delta_time
    end
  end

  class Heater < ThermalExchanger
    def heat!( delta_time )
      act delta_time
    end
  end

  class Cooler < ThermalExchanger
    def cool!( delta_time )
      act( delta_time, -power_output )
    end
  end
end

设置了一个受控环境 mixin,让我们使用它创建一个 House 类。

class House
  include ControlledEnvironment # including a mixin

  # a house has air, one heater, and one cooler
  attr_reader :heater, :cooler, :air

  def initialize( heater_power: 1.kW, cooler_power: 1.kW,
                  air_volume: 500.m(3) )
    @air = Air.new volume: air_volume
    @heater = Heater.new( heater_power, @air )
    @cooler = Cooler.new( cooler_power, @air )
  end

  # its temperature is defined as air temperature
  def temperature
    "#{(air.temperature - SY::TRIPLE_POINT_OF_WATER).to_f} centigrade"
  end

  # and, given current heating / cooling power settings, we can step forward in time
  def step( delta_time=1.min )
    heater.heat! delta_time
    cooler.cool! delta_time
    puts "After #{delta_time.in( :min )} minutes, the temperature is #{temperature}"
  end
end

现在我们可以玩了:

house = House.new heater_power: 1.5.kW, cooler_power: 1.kW, air_volume: 1500.m(3)
house.temperature
#=> "20.0 centigrade"
house.step 1.h
#=> After 60 minutes, the temperature is 21.040302385090797 centigrade
house.step 1.h
#=> After 60 minutes, the temperature is 22.08429649473362 centigrade
house.heater.power_output = 0.kW # turn off the heater, for instance
#=> #<±Magnitude: 0.W >
house.step 30.min
#=> After 30 minutes, the temperature is 21.013285968306377 centigrade
100.times do house.step end
于 2013-05-05T18:10:06.940 回答
2

如果我正确阅读了您的问题,要调用实例方法,您首先必须创建该类的实例。在这种情况下,您有一个 House 类。

my_house = House.new(72, true, false)

my_house.update_temperature!

话虽如此,您的代码存在一些问题。

then首先,在 if 语句中使用通常是不好的做法。相反,您可以通过替换来做这样的事情

if @heater then @current_temp += 1

@current_temp += 1 if @heater

或者

if @heater
  @current_temp += 1
end

其次,我可能会将方法重写为update_temperature!if/else 语句。我会留给你学习。

最后,您需要在初始化方法中添加一个end,以便您的初始化方法看起来像

   def initialize(current_temp, heater, air_cond)
     @current_temp = current_temp
     @heater = heater
     @air_cond = air_cond
   end

最后我想说你应该看看Why's Poignant Guide to Ruby。这将极大地帮助您解决您正在处理的一些问题。

于 2013-05-05T17:51:48.360 回答