Created
July 16, 2013 19:57
-
-
Save BooneTeam/6012107 to your computer and use it in GitHub Desktop.
This tests for duplicate entries and suggests a name with the next greatest number attached to the end. It then prompts the user to enter a new one or say yes to the one suggested.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module NewEntryDupTest | |
attr_accessor :entry, :old_entries | |
# def initialize(*args) | |
# @old_entries ||= [] | |
# @entry = :entries | |
# end | |
@@old_entries = ['Homer'] | |
def self.set(old_entries) | |
@@old_entries = old_entries | |
end | |
def self.get | |
@@old_entries | |
end | |
def new_entry(entry) | |
@@old_entries.include?(entry) ? suggest_name(entry) : register_entry(entry) | |
end | |
def register_entry(newest_entry) | |
@@old_entries << newest_entry | |
"#{newest_entry}" | |
end | |
def suggest_name(entry) | |
until @@old_entries.include?("#{entry + ( nums ||= 0 ).to_s }") == false | |
nums = increase_nums(nums) | |
end | |
prompt_user("#{entry + ( nums ||= 0 ).to_s }") | |
end | |
def increase_nums(num) | |
num += 1 | |
end | |
def prompt_user(new_entry) | |
puts "Already Taken,type yes if you would like to save as #{new_entry}" | |
input = gets.chomp.downcase | |
if input == "yes" | |
register_entry(new_entry) | |
else | |
puts "Please retry with a different name for #{self.class}" | |
try_again = self.class.new | |
input = gets.chomp | |
try_again.new_entry(input) | |
end | |
end | |
end | |
class UserName | |
include NewEntryDupTest | |
end | |
user = UserName.new | |
NewEntryDupTest.set(["Bart0","Bart4", "Bart5", "Bart6", "Bart7", "Bart8", "Bart9", "Bart10", | |
"Lisa", "Marge", "Homer", "Bart", "Bart1", "Bart2", "Bart3", | |
"Bart11", "Bart12"]) | |
user.new_entry('Bart') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment