Skip to content

Instantly share code, notes, and snippets.

@samtalks
Created October 1, 2013 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samtalks/6778429 to your computer and use it in GitHub Desktop.
Save samtalks/6778429 to your computer and use it in GitHub Desktop.
# You need to program the "Take a Number" feature for a deli. At the start, the deli is empty and is represented by an empty array.
# Build a method that a new customer will use when entering our deli. The method, take_a_number, should accept the current line of people, along with the new person's name, and return their position in line (and no 0 index, these are normal people, if they are 7th in line, tell them that, not 6).
def take_a_number(place, person)
place << person
place.length.to_s
end
# Build a method now_serving. This method should call out (via puts) the next person in line and remove them from the line.
def now_serving(place)
"Currently serving #{place[0]}"
end
# Build a method line that shows people their current place in line.
def line(place)
str = "The line is currently: "
place.each do |person|
str << (place.index(person)+1).to_s + ". " + person + " "
end
str
end
katz_deli = []
# Example usage:
p take_a_number(katz_deli, "Ada") #=> "1"
p take_a_number(katz_deli, "Grace") #=> "2"
p take_a_number(katz_deli, "Kent") #=> "3"
p line(katz_deli) #=> "The line is currently: 1. Ada 2. Grace 3. Kent"
p now_serving(katz_deli) #=> "Currently serving Ada"
p line(katz_deli) #=> "The line is currently: 1. Grace 2. Kent"
p take_a_number(katz_deli, "Matz") #=> "3"
p line(katz_deli) #=> "The line is currently: 1. Grace 2. Kent 3. Matz"
p now_serving(katz_deli) #=> "Currently serving Grace"
p line(katz_deli) #=> "1. Kent 2. Matz"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment