A => conducir +30 -0
@@ 1,30 @@
+#!/usr/bin/env ruby
+# frozen_string_literal: true
+
+require_relative 'vehiculo'
+
+vehiculo = Vehiculo.new
+accion = ''
+
+puts 'Bienvenido a su vehículo, escriba `salir` para finalizar el programa'
+
+while accion != 'salir'
+ puts 'Elija una acción: acelerar, frenar o girar'
+ accion = gets.strip
+
+ next if accion == 'salir'
+
+ if ['acelerar', 'frenar'].include? accion
+ puts "¿Cuánto deseas #{accion}? (en km/h)"
+ param = gets.strip.to_i
+ elsif accion == 'girar'
+ puts 'Hacia dónde deseas girar? (izquierda, derecha o centro)'
+ param = gets.strip.to_sym
+ else
+ puts 'Acción inválida'
+ end
+
+ vehiculo.public_send(accion, param)
+ puts vehiculo
+ sleep 2
+end
A => foco.rb +8 -0
@@ 1,8 @@
+# frozen_string_literal: true
+
+require_relative 'girador'
+
+# Un foco con la unica funcionalidad de girar
+class Foco
+ include Girador
+end
A => girador.rb +22 -0
@@ 1,22 @@
+# frozen_string_literal: true
+
+# Provee la funcionalidad de un elemento que gira en 180 grados
+module Girador
+ attr_reader :direccion
+
+ DIRECCIONES = { derecha: 0, centro: 90, izquierda: 180 }.freeze
+ private_constant :DIRECCIONES
+
+ def girar(direccion = :centro)
+ if DIRECCIONES[direccion]
+ @direccion = DIRECCIONES[direccion]
+ elsif direccion >= 0 && direccion <= 180
+ @direccion = direccion
+ else
+ raise ArgumentError
+ end
+ rescue ArgumentError
+ print 'Giro inválido, puede ser de 0 a 180,'
+ puts ' o un símbolo :izquierda, :derecha o :centro'
+ end
+end
A => vehiculo.rb +60 -0
@@ 1,60 @@
+# frozen_string_literal: true
+
+require_relative 'girador'
+
+# Provee la funcionalidad basica de un vehiculo
+class Vehiculo
+ include Girador
+
+ VELOCIDAD_MAX_VEHICULO = 300
+ VELOCIDAD_MAX_CARRETERA = 120
+ VELOCIDAD_MAX_URBANA = 50
+
+ attr_reader :velocidad
+
+ def initialize(velocidad = 0)
+ @velocidad = velocidad
+ @direccion = DIRECCIONES[:centro]
+ end
+
+ def acelerar(km_h)
+ @velocidad += km_h
+ @velocidad = VELOCIDAD_MAX_CARRETERA if @velocidad > VELOCIDAD_MAX_VEHICULO
+ self
+ end
+
+ def frenar(km_h)
+ @velocidad -= km_h
+ @velocidad = 0 unless @velocidad >= 0
+ self
+ end
+
+ def to_s
+ "Estás #{descripcion_velocidad} #{descripcion_direccion}"
+ end
+
+ protected
+
+ def descripcion_velocidad
+ desc = case @velocidad
+ when 0
+ 'estacionado'
+ when 0..VELOCIDAD_MAX_URBANA
+ 'conduciendo a velocidad urbana'
+ when VELOCIDAD_MAX_URBANA..VELOCIDAD_MAX_CARRETERA
+ 'conduciendo a velocidad de carretera'
+ else 'a exceso de velocidad' end
+ desc == 'estacionado' ? desc : desc + " a #{@velocidad} km/h"
+ end
+
+ def descripcion_direccion
+ case @direccion
+ when DIRECCIONES[:derecha]...DIRECCIONES[:centro]
+ 'girando hacia la derecha'
+ when DIRECCIONES[:centro]
+ 'en dirección recta'
+ when (DIRECCIONES[:centro] + 1)..DIRECCIONES[:izquierda]
+ 'girando hacia la izquieda'
+ end
+ end
+end