From 6aad109f32a68d50ec7c3f62725b44808a0c9f68 Mon Sep 17 00:00:00 2001 From: mjvezzani Date: Sat, 25 Jan 2014 19:44:05 -0500 Subject: [PATCH] Use Struct to create a Passenger object on the fly, assign details to the passenger, including a specific train, and output passenger information, as well as train information, in an easy to read format. --- recipe.rb | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/recipe.rb b/recipe.rb index d4b2000..9186455 100644 --- a/recipe.rb +++ b/recipe.rb @@ -1,17 +1,29 @@ -ingredients = {} -ingredients[:avocados] = 4 -ingredients[:jalapenos] = 2 +# Method(s) to prettify output format -Recipe = Struct.new(:ingredients, :method) +def prettify(obj) + obj.to_s.capitalize.gsub(/_/, ' ') +end + +train = {} +train[:current_city] = "St. Louis" +train[:number_of_engines] = 3 +train[:number_of_cars] = 10 +train[:caboose] = true + +Passenger = Struct.new(:passenger_information, :train) -recipe = Recipe.new( {avacados: 4, jalapenos: 2}, ["Peel / Slice Avocados", "Chop jalapenos into small dice"]) +passenger = Passenger.new( {name: "Mike Vezzani", city_of_origin: "St. Louis", + travelling_to: "San Francisco"}, {current_city: "St. Louis", + number_of_engines: 3, + number_of_cars: 10, + caboose: true}) -puts "ingredients" -recipe.ingredients.each do |key, value| - puts "* #{key}: #{value}" +puts "---- Passenger Information ----" +passenger.passenger_information.each do |k, v| + puts "#{prettify(k)}: #{v}" end -puts "\nMethod" -recipe.method.each_with_index do |step, index| - puts "#{index+1}. #{step}" +puts "---- Train Information ----" +passenger.train.each do |k, v| + puts "#{prettify(k)}: #{v}" end \ No newline at end of file