title: How i can validate attr in Ruby
body: <p>How I can validate attr in Ruby? </p>
  
  <p>My file airplane.rb</p>
  
  <pre><code>class Airplane
  
  include Validatable
  
  attr_reader   :aircraft_type, :weight
  attr_accessor :speed, :altitude, :course
  
  def initialize(aircraft_type, options={})
      @aircraft_type  = aircraft_type.to_s 
      @course                 = options[:course]     || random_course  
      @weight         = options[:weight]     || rand(1...1000)
      @speed          = options[:speed]      || rand(1...500)
      @apltitude      = options[:apltitude]    || rand(50...3000)
      @position_x         =   options[:position_x] || rand(1...3000)
    @position_y           = options[:position_y] || rand(1...3000)
    check_course
  end 
  
  def position
   @position = [@position_x, @position_y]
  end
  
  def check_course
  if @course &lt; 1
      @course = 1
      puts "Invalid course. Set min"
  elsif @course &gt; 360
      @course = 360
      puts "Invalid course. Set max"
  else
      @course = @course
      end
  end
  
  def random_course
  @course = rand(1..360)
  end
  </code></pre>
  
  <p>end</p>
  
  <p>My file validatable.rb where all values ​​must be checked </p>
  
  <pre><code>module Validatable
  
  @@validations={}
  # I need receive = {presence: [:weight, :length], aircraft_type: [:length]}
  
  def self.validates_presence_of(*attrs)
      @@validations[:presence] = attrs
  end
  
  def validate 
      @@validations.each do |v, fields| 
          fields.each {|field_name| self.send("validate_#{v}_of", field_name)}
      end
  end
  
  private 
  
      def validate_presence_of(field_name)
      end
  </code></pre>
  
  <p>end</p>
  
  <p>My file init.rb with airplanes attr </p>
  
  <pre><code>airplane1 = Airplane.new("Boeing 74", course: 600, speed: 300, apltitude: 300)
  airplane2 = Airplane.new("Boeing 700", course: 250, speed: 300, apltitude: 300)
  airplane3 = BigAirplane.new("Boeing 707", weight: 50, speed: 300, apltitude: 400)
  </code></pre>
  
  <p>How I can finish validatable.rb to validate each value in each airplane?</p>