Oof. We are officially in the holiday season, if not necessarily the holiday spirit. I apologize for almost immediately falling off the wagon of posting, but a nice, relaxing trip to San Diego threw me off my game. Anyway, starting where I left off, here’s Day 8!
This one was interesting, in that it’s relatively useful, and features some realistic math. It asks you to write a program that prompts for the dimensions of a room, specifying the units, either feet or meters. It then calculates the area of the room in the units specified and tells you the result.
The book includes the equation for converting between feet and meters, and my solution first prompts for which unit, and then uses a ternary function to determine the rate of conversion. It then uses that rate to generate the desired output:
This seems to be working well, but I think I can refactor it a bit. In this one, I specifically ask for two things: integers, and units. Now that I have the Useful Methods file, I can require that, and remove the getInteger(prompt) method. Since I’ve been asking the user to choose specific strings in a few of these challenges, I’m also going to add a chooseOne(prompt,optionsArray) method to my Useful Methods file, that way I can reuse it later!
def chooseOne(prompt,optionsArray)
begin
puts prompt
choice = gets.chomp.downcase
raise if (optionsArray.include? choice) == false
rescue
puts “Please enter one of the options:”
optionsArray.each { |option| puts option }
retry
end
return choice
end
I started off planning on just passing three arguments to this method; a prompt, option A, and option B, but then I figured it would be more versatile if I could just pass in a list of options, and check to see if the option chosen by the user was one of them. Once I got the .include? Method working, it seems to have worked like a charm! Thanks for reading, and that’s day 8 of 57!