Skip to content

Instantly share code, notes, and snippets.

@patrickberkeley
Created June 16, 2009 03:56
Show Gist options
  • Save patrickberkeley/130516 to your computer and use it in GitHub Desktop.
Save patrickberkeley/130516 to your computer and use it in GitHub Desktop.
# Method borrowed from deprecated Rails Inflector class
def ordinalize(number)
if (11..13).include?(number.to_i % 100)
"#{number}th"
else
case number.to_i % 10
when 1; "#{number}st"
when 2; "#{number}nd"
when 3; "#{number}rd"
else "#{number}th"
end
end
end
# Check to see if the floor entered exists in the building
def floor?(floor, array)
if array.include?(floor)
true
else
error(floor)
Process.exit
end
end
# Move the elevator from start floor to end floor
def move_elevator(current_floor, terminal_floor)
puts "Going from the " + ordinalize(current_floor) + " floor to the " + ordinalize(terminal_floor) + " floor."
end
# Let user know floor doesn't exist in the building
def error(floor)
puts "This building doesn't have a " + ordinalize(floor) + " floor."
end
puts "Pop quiz!"
puts "How many floors are in the building?"
total_floors = gets.chomp.to_i
floor_range = 1..total_floors # Create a range of floors
floor_array = floor_range.to_a # Convert the range to an array
puts "What floor are you on?"
current_floor = gets.chomp.to_i
floor?(current_floor, floor_array) # Check to see if entry exists in building
puts "What floor are you going to?"
terminal_floor = gets.chomp.to_i
floor?(terminal_floor, floor_array) # Check to see if entry exists in building
move_elevator(current_floor, terminal_floor) # Move from start to end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment