English 中文(简体)
初始初始化散列的最大值最小值( 最大值)
原标题:Min & max values for initialize hash
  • 时间:2012-05-24 18:09:31
  •  标签:
  • ruby
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 中的散列的最小和最大允许值?

示例:

airplane1 = Airplane.new("Boeing 74", course: 200)
p radar1.airplanes
=> [#<Airplane:0x000000023dfc78 @aircraft_type="Boeing 74", @course="200%"]

但如果我设定航程价值370, 飞机1 不应该工作

最佳回答

这可能是重构的我确定 但我想,这就是我所想的

class Plane

  attr_reader :weight, :aircraft_type
  attr_accessor :speed, :altitude, :course

  def initialize(aircraft_type, options = {})
    @aircraft_type = aircraft_type.to_s 
    @course = options[:course] || random_course
    check_course  
  end

  def check_course
   if @course < 1 or @course > 360
      @course = 1
      puts "Invalid course. Set min"
     elsif @course > 360
      @course = 360
      puts "Invalid course. Set max"
     else
      @course = @course
     end
   end

   def random_course
    @course = rand(1..360)
   end

end
问题回答

course 是一个角度, 不是吗? 它不应该是 0...360 有效范围吗? 以及为什么最终的“% ”? 为什么用字符串而不是整数工作?

总之,这就是我写:

@course = ((options[:course] || rand(360)) % 360).to_s + "%"

我想你的意思是,你不想让人们通过类似 的东西,比如 options options ,如果它是无效的,你想错误退出。如果是这样的话,你可以测试它是否在范围内:

def initialize(aircraft_type, options = {})
  @aircraft_type  = aircraft_type.to_s 
  allowed_range = 1...360
  passed_course = options[:course]
  @course = case passed_course
    when nil
      "#{rand allowed_range}%"
    when allowed_range
      "#{passed_course}%"
    else 
      raise ArgumentError, "Invalid course: #{passed_course}"
  end
end 




相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...