55502f40dc8b7c769880b10874abc9d0

What would be the best or a good idiomatic way to display a menu through the use of a module?! Im having a main menu which have 2 submenus that you can navigate to through the main menu. I want the main menu only to show first when you start the program and then by choosing either the third choice or the fourth one you get to the menus under them. my code so far looks like the example below but Im not really working it out. Any suggestions?! Thanks!

module Menus
  
 def self.getValidPositiveNumber
		input = gets.chomp
		
		while (input.to_i.to_s != input && input.to_f.to_s != input) do	
			puts "Ogiltig data. Försök igen."
			input = gets.chomp
		end
		
		# Är talet större än 0?
		number = input.to_f
		if (number <= 0)
			puts "Du kan inte ange negativt värde."
			getValidPositiveNumber
		end
		return number
	end
	
	def self.get_valid_input(valid_options)
		
		input = gets.chomp

		# loopa tills ett giltigt värde angetts
		while (!valid_options.include?(input) && !valid_options.include?(input.to_i))	
			# både Range och Array har include?
			puts "Ogiltigt värde. Skriv in ett nytt alternativ mellan " + valid_options.inspect
			input = gets.chomp
		end
		return input
	
	end
	
	
	class Menu
	
		attr_reader  :valid_options_range, :menu_string
	 
		def initialize(valid_options_range, menu_string)
			@valid_options_range = valid_options_range
			@menu_string = menu_string
		end
		
		

		def do_menu_action(action)
			raise "Måste anropas i någon subklass!"
		end
		
	
		def to_s
			return @menu_string
		end
	end


  MAIN_MENU = <<END 
"---------------------------" 
  Welcome to Ruby Camping!
      Menu
1. Checkin
2. Checkout
3. Lists
4. Economy
5. Exit

What do you want to do?
"---------------------------"
END


#if choice >= menu_options.length or choice < 0
 #puts "Invalid choice"

def make_menu_choice(choice)

   case choice
        when 1:
          $camping.check_in
        when 2:
           $camping.check_out
        when 3:
          $current_menu = LISTS_MENU
        when 4:
          $current_menu = ECONOMY_MENU
        when 5:
          exit
       end
    end
 

  LISTS_MENU = <<END
"---------------------------"   
-- 1. List current guests --
-- 2. List all guests --
--                          --
-- 0. Back to Main menu      --
------------------------------"
END


def make_menu_choice(choice) 
   case choice
        when 1:
          $camping
        when 2:
          $camping.all_guests
        when 0:
          $current_menu = MAIN_MENU
       end
    end

   ECONOMY_MENU = <<END
 "---------------------------"   
 -- 1. List current guests --
 -- 2. List all guests --
 --                          --
 -- 0. Back to Main menu      --
 ------------------------------"
END

end    

puts Menus::MAIN_MENU 
if Menus == 3 then LISTS_MENU = Lists_Menu.new 
elsif Menus == 4 then ECONOMY_MENU = Economy_Menu.new
end
__END__

Refactorings

No refactoring yet !

2163d4cf4f5c70472dbc92e0e5959053

andrew

September 1, 2010, September 01, 2010 17:39, permalink

No rating. Login to rate!

Hi..I don't know why do u has a module Menus (with only 2 methods) and a class Menu too..try encapsulate all inside a module or a class...this code look a bit ugly..if you wanna works with class you can create a object for every menu and the constructor (def initialize) would receive the name parameter, and a hash{} with the submenus...this hash would be with the key the name to show..... and value would be a object (remember every menu would be a object)...please dont put "valid_options_range" inside the constructor..because is confusing and isn't recommended, you need other method for show menu (pretty display) and other for receive the key press and check which kay was pressed(checking is a correct value too...here work with range)...this would be a simple way, and it will be pretty expansible and scalable...also object orientation....if you prefer work with modules..the way would be working with lambdas (it's the only simple way I can think for this now)...remember when you work with modules no works with object...for me when I work with modules always work with procs and lambdas or I use these for "complete" my class...in your example a module for check corrects options (the module Menus) could have sense but no if only have two methods..It's better option encapsulate these inside the class...if you wanna put new functions inside the module can leave it but would be better rename this to CheckMenus or a name more clear...I hope this can help you....bye and good luck...

55502f40dc8b7c769880b10874abc9d0

tragicdancer.myopenid.com

September 3, 2010, September 03, 2010 15:35, permalink

No rating. Login to rate!

Thank you for your answer but Im afraid Im not quite following you. Im new at Ruby so it does not come easy for me the language. Could you not please give me an example of code to illustrate what you mean?! Many thanks!

Your refactoring





Format Copy from initial code

or Cancel