class Airplane
attr_reader :weight, :aircraft_type
attr_accessor :speed, :altitude, :course
def initialize(aircraft_type, options = {})
@aircraft_type = aircraft_type.to_s
@course = options[:course.to_s + "%"] || rand(1...360).to_s + "%"
end
如何在1到360之间使用initialize
中散列的最小和最大允许值?用于初始化散列的最小值和最大值
例子:
airplane1 = Airplane.new("Boeing 74", course: 200)
p radar1.airplanes
=> [#<Airplane:0x000000023dfc78 @aircraft_type="Boeing 74", @course="200%"]
但如果我设置为当然值370,airplane1不应该工作
你的问题不是很清楚。什么是“允许”值?你指的是什么哈希?你期望什么样的散列的最终值得到某些输入? –
好的,所以你想确保'options [:course]'在一个指定的值范围内?如果不是,会发生什么? (“不工作”不是很清楚。) –
是的,我想要:具有指定数值范围的课程,如果不是 - 会自动插入最大值 – Savroff