如何避免在 Ruby 中使用 class 变量
How can I avoid using class variables in Ruby
我有一段使用 class 变量的代码。我读到在 Ruby.
中通常应避免使用 class 变量
class 变量是 @@cost
和 @@kwh
。
如何在不使用 class 变量的情况下重写以下内容?
class Device
attr_accessor :name, :watt
@@cost = 0.0946
def initialize(name, watt)
@name = name
@watt = watt
end
def watt_to_kwh(hours)
@@kwh = (watt / 1000) * hours
end
def cost_of_energy
puts "How many hours do you use the #{self.name} daily?"
hours = gets.chomp.to_i
self.watt_to_kwh(hours)
daily_cost = @@kwh * @@cost
montly_cost = daily_cost * 30
puts "Dayly cost: #{daily_cost}€"
puts "montly_cost: #{montly_cost}€"
end
end
@@cost
表现得更像一个 常量 (即它不会在运行时改变),所以你应该使用一个:
COST = 0.0946
@@kwh
应该是一个实例变量,因为它只在实例化对象中使用,所以你可以使用 @kwh
代替:
@kwh = (watt / 1000) * hours
而daily_cost = @@kwh * @@cost
会变成:
daily_cost = @kwh * COST
这将避免使用 class 变量,但您也可以完全消除 @kwh
,因为您不会在其他任何地方使用它。
所以,而不是:
def watt_to_kwh(hours)
@kwh = (watt / 1000) * hours
end
你可以这样做:
def watt_to_kwh(hours)
(watt / 1000) * hours
end
并在 cost_of_energy
方法中像这样使用它:
def cost_of_energy
puts "How many hours do you use the #{self.name} daily?"
hours = gets.chomp.to_i
daily_cost = watt_to_kwh(hours) * COST
montly_cost = daily_cost * 30
puts "Dayly cost: #{daily_cost}€"
puts "montly_cost: #{montly_cost}€"
end
试试这个。
class Device
singleton_class.send(:attr_accessor, :cost_per_kwh)
def initialize(name, watts)
@name = name
@watts = watts
end
def daily_cost(hours_per_day)
self.class.cost_per_kwh * kwh_per_day(hours_per_day)
end
def monthly_cost(hours_per_day)
30 * daily_cost(hours_per_day)
end
private
def kwh_per_day(hours_per_day)
hours_per_day * @watts / 1000
end
end
singleton_class.send(:attr_accessor, :cost_per_kwh)
为 class 实例变量 @cost_per_kwh
.
创建一个 setter 和 getter
首先,获取并保存每千瓦时的成本,这将用于计算所有相关设备的成本。
puts "Please enter the cost per kwh in $"
Device.cost_per_kwh = gets.chomp.to_f
假设
Device.cost_per_kwh = 0.0946
计算每个感兴趣的设备的成本。
puts "What is the name of the device?"
name = gets.chomp
puts "How many watts does it draw?"
watts = gets.chomp.to_f
假设
name = "chair"
watts = 20000.0
我们现在可以创建一个 class 实例。
device = Device.new(name, watts)
#=> #<Device:0x007f9d530206f0 @name="chair", @watts=20000.0>
最后,获取每天的小时数,这是给定设备的未来成本计算中唯一可能发生变化的变量。
puts "How many hours do you use the #{name} daily?"
hours_per_day = gets.chomp.to_f
最后,假设
hours_per_day = 0.018
然后我们可以计算成本。
puts "Daily cost: $#{ device.daily_cost(hours_per_day)}"
Daily cost: [=18=].034056€
puts "Monthly_cost (30 days/month): $#{ 30 * device.daily_cost(hours_per_day) }"
Monthly_cost (30 days/month): .0216800000000001
假设情况发生变化1并且设备的使用增加。我们只需要每天更新小时数。例如,
puts "How many hours do you use the #{name} daily?"
hours_per_day = gets.chomp.to_f
假设现在
hours_per_day = 1.5
然后
puts "Daily cost: $#{ device.daily_cost(hours_per_day)}"
Daily cost: .838
puts "Monthly_cost (30 days/month): $#{ 30 * device.daily_cost(hours_per_day) }"
Monthly_cost (30 days/month): .14
1 以选举新总统为例
我有一段使用 class 变量的代码。我读到在 Ruby.
中通常应避免使用 class 变量class 变量是 @@cost
和 @@kwh
。
如何在不使用 class 变量的情况下重写以下内容?
class Device
attr_accessor :name, :watt
@@cost = 0.0946
def initialize(name, watt)
@name = name
@watt = watt
end
def watt_to_kwh(hours)
@@kwh = (watt / 1000) * hours
end
def cost_of_energy
puts "How many hours do you use the #{self.name} daily?"
hours = gets.chomp.to_i
self.watt_to_kwh(hours)
daily_cost = @@kwh * @@cost
montly_cost = daily_cost * 30
puts "Dayly cost: #{daily_cost}€"
puts "montly_cost: #{montly_cost}€"
end
end
@@cost
表现得更像一个 常量 (即它不会在运行时改变),所以你应该使用一个:
COST = 0.0946
@@kwh
应该是一个实例变量,因为它只在实例化对象中使用,所以你可以使用 @kwh
代替:
@kwh = (watt / 1000) * hours
而daily_cost = @@kwh * @@cost
会变成:
daily_cost = @kwh * COST
这将避免使用 class 变量,但您也可以完全消除 @kwh
,因为您不会在其他任何地方使用它。
所以,而不是:
def watt_to_kwh(hours)
@kwh = (watt / 1000) * hours
end
你可以这样做:
def watt_to_kwh(hours)
(watt / 1000) * hours
end
并在 cost_of_energy
方法中像这样使用它:
def cost_of_energy
puts "How many hours do you use the #{self.name} daily?"
hours = gets.chomp.to_i
daily_cost = watt_to_kwh(hours) * COST
montly_cost = daily_cost * 30
puts "Dayly cost: #{daily_cost}€"
puts "montly_cost: #{montly_cost}€"
end
试试这个。
class Device
singleton_class.send(:attr_accessor, :cost_per_kwh)
def initialize(name, watts)
@name = name
@watts = watts
end
def daily_cost(hours_per_day)
self.class.cost_per_kwh * kwh_per_day(hours_per_day)
end
def monthly_cost(hours_per_day)
30 * daily_cost(hours_per_day)
end
private
def kwh_per_day(hours_per_day)
hours_per_day * @watts / 1000
end
end
singleton_class.send(:attr_accessor, :cost_per_kwh)
为 class 实例变量 @cost_per_kwh
.
首先,获取并保存每千瓦时的成本,这将用于计算所有相关设备的成本。
puts "Please enter the cost per kwh in $"
Device.cost_per_kwh = gets.chomp.to_f
假设
Device.cost_per_kwh = 0.0946
计算每个感兴趣的设备的成本。
puts "What is the name of the device?"
name = gets.chomp
puts "How many watts does it draw?"
watts = gets.chomp.to_f
假设
name = "chair"
watts = 20000.0
我们现在可以创建一个 class 实例。
device = Device.new(name, watts)
#=> #<Device:0x007f9d530206f0 @name="chair", @watts=20000.0>
最后,获取每天的小时数,这是给定设备的未来成本计算中唯一可能发生变化的变量。
puts "How many hours do you use the #{name} daily?"
hours_per_day = gets.chomp.to_f
最后,假设
hours_per_day = 0.018
然后我们可以计算成本。
puts "Daily cost: $#{ device.daily_cost(hours_per_day)}"
Daily cost: [=18=].034056€
puts "Monthly_cost (30 days/month): $#{ 30 * device.daily_cost(hours_per_day) }"
Monthly_cost (30 days/month): .0216800000000001
假设情况发生变化1并且设备的使用增加。我们只需要每天更新小时数。例如,
puts "How many hours do you use the #{name} daily?"
hours_per_day = gets.chomp.to_f
假设现在
hours_per_day = 1.5
然后
puts "Daily cost: $#{ device.daily_cost(hours_per_day)}"
Daily cost: .838
puts "Monthly_cost (30 days/month): $#{ 30 * device.daily_cost(hours_per_day) }"
Monthly_cost (30 days/month): .14
1 以选举新总统为例