From: rwtnorton@... Date: 2006-12-31T07:53:08+09:00 Subject: Re: Newbie: It works, how can I make it better? > puts "" > puts "Conversion Program" > puts "------------------\n" > > puts "Would you like to convert:" > puts "1. F to C" > puts "2. C to F" > puts"Enter your choice now: " > choice = gets.chomp.to_i > > if choice == 1 > puts "Enter a temperature in F:" > fahr = gets.chomp.to_f > cent = ( (fahr - 32) / 1.8 ) > puts fahr.to_s + " F is " + cent.round.to_s + " C." > > elsif choice == 2 > puts "Enter a temperature in C:" > cent = gets.chomp.to_f > fahr = ( (cent * 1.8) + 32 ) > puts cent.to_s + " C is " + fahr.round.to_s + " F." > else > puts "That is a not a valid choice. Good bye!" > end > I'm using Ruby, but I'll take a stab at it (learn from my mistakes). Well, I started off OK (using classes for temp conversion) and then things got ugly (probably putting too much in the manager class). At any rate, here it is. (aka, here's how not to do it. ;-) #! /usr/bin/env ruby class TemperatureConverter attr_reader :from_unit, :to_unit, :conversion def initialize( from_unit='', to_unit='', conversion=proc {|x| x } ) @from_unit = from_unit @to_unit = to_unit @conversion = conversion end def convert( arg ) @conversion.call( arg ) end end class FahrenheitToCelsiusConverter < TemperatureConverter def initialize super( 'F', 'C', proc {|f| (f - 32.0) / 1.8 } ) end end class CelsiusToFahrenheitConverter < TemperatureConverter def initialize super( 'C', 'F', proc {|c| (c * 1.8) + 32 } ) end end class TemperatureConversionManager attr_reader :prompt def initialize( *converters ) @converters = converters @prompt = <= @converters.size puts "That is not a valid choice. Goodbye." else prompt_and_display_conversion( @converters[ user_choice ] ) end end def choose_converter gets.chomp.to_i end def get_temperature gets.chomp.to_f end def temperature_prompt( converter ) "Please enter temperature in #{converter.from_unit}: " end def prompt_and_display_conversion( converter ) print temperature_prompt( converter ) user_input = get_temperature converted_temp = converter.convert( user_input ) puts <