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:
-
mach = Mach.new()
-
machSpeed = mach.to_mach( 655.7144, 10 ).to_s()
-
=> 0.539956736381214
-
kmhSpeed = mach.to_kmh( 1, 10 ).to_s()
-
=> 1214.3832196531
RUBY:
-
=begin rdoc
-
Convert between Km/h and Mach speeds
-
-
By Chris Cummer, 2006
-
version 1.0
-
=end
-
class Mach
-
require 'bigdecimal/math'
-
-
# ------------------------------ Initialization ------------------------------ #
-
-
-
def initialize
-
@knots_to_km_ratio = 0.5399568
-
@kelvin_to_celcius = 273.15
-
@the_magic_constant = 38.967854
-
end
-
-
-
# ------------------------------ Public Methods ------------------------------ #
-
-
=begin rdoc
-
Convert from Km/h to Mach
-
=end
-
def to_mach( pAirspeedKmh, pOutsideAirTempC )
-
return ( @knots_to_km_ratio * pAirspeedKmh ) / ( @the_magic_constant * Math.sqrt( pOutsideAirTempC + @kelvin_to_celcius ) )
-
end
-
-
-
=begin rdoc
-
Convert from Mach to Km/h
-
=end
-
def to_kmh( pAirspeedMach, pOutsideAirTempC )
-
return ( pAirspeedMach * ( @the_magic_constant * Math.sqrt( pOutsideAirTempC + @kelvin_to_celcius ) ) ) / @knots_to_km_ratio
-
end
-
end
-
-
-
-
-
-
# ------------------------------ Unit Testing ------------------------------ #
-
-
-
if __FILE__ == $0
-
mach = Mach.new()
-
puts "655.7144 Km/h = Mach " <<mach.to_mach( 655.7144, 10 ).to_s()
-
puts "Mach 1 = " <<mach.to_kmh( 1, 10 ).to_s() <<" Km/h"
-
end