Ruby Class: Mach Speed

2008-01-17 Update: You could use the code below but for the latest, greatest version this is now hosted on RubyForge: http://rubyforge.org/projects/machspeedcalc/.

I suggest taking a gander at this little project there instead.


Mach: A Ruby class for calculating Mach speed from airspeed (in Km/h) and outside air temperature (in °C).

Usage:

RUBY:
  1. mach = Mach.new()
  2. machSpeed = mach.to_mach( 655.7144, 10 ).to_s()
  3. => 0.539956736381214
  4. kmhSpeed = mach.to_kmh( 1, 10 ).to_s()
  5. => 1214.3832196531

RUBY:
  1. =begin rdoc
  2.   Convert between Km/h and Mach speeds
  3.  
  4.   By Chris Cummer, 2006
  5.   version 1.0
  6. =end
  7. class Mach
  8.   require 'bigdecimal/math'
  9.  
  10.   # ------------------------------ Initialization ------------------------------ #
  11.  
  12.  
  13.   def initialize
  14.     @knots_to_km_ratio = 0.5399568
  15.     @kelvin_to_celcius = 273.15
  16.     @the_magic_constant = 38.967854
  17.   end
  18.  
  19.  
  20.   # ------------------------------ Public Methods ------------------------------ #
  21.  
  22. =begin rdoc
  23.   Convert from Km/h to Mach
  24. =end
  25. def to_mach( pAirspeedKmh, pOutsideAirTempC )
  26.     return ( @knots_to_km_ratio * pAirspeedKmh ) / ( @the_magic_constant * Math.sqrt( pOutsideAirTempC + @kelvin_to_celcius )  )
  27.   end
  28.  
  29.  
  30. =begin rdoc
  31.   Convert from Mach to Km/h
  32. =end
  33. def to_kmh( pAirspeedMach, pOutsideAirTempC )
  34.     return ( pAirspeedMach * ( @the_magic_constant * Math.sqrt( pOutsideAirTempC + @kelvin_to_celcius ) ) ) / @knots_to_km_ratio
  35.   end
  36. end
  37.  
  38.  
  39.  
  40.  
  41.  
  42. # ------------------------------ Unit Testing ------------------------------ #
  43.  
  44.  
  45. if __FILE__ == $0
  46.   mach = Mach.new()
  47.   puts "655.7144 Km/h = Mach " <<mach.to_mach( 655.7144, 10 ).to_s()
  48.   puts "Mach 1 = " <<mach.to_kmh( 1, 10 ).to_s() <<" Km/h"
  49. end