Skip to content

Instantly share code, notes, and snippets.

@atleastimtrying
Created September 23, 2014 10:38
Show Gist options
  • Save atleastimtrying/8ce60ccca753972b2356 to your computer and use it in GitHub Desktop.
Save atleastimtrying/8ce60ccca753972b2356 to your computer and use it in GitHub Desktop.
a while?
I need 100 usernames some may fail the test
def get_usernames
response = []
#loop
name = random_name()
response << random_name unless User.find(username: name)
#end loop
response
end
@MrJaba
Copy link

MrJaba commented Sep 23, 2014

def get_usernames
  response = []
  begin
    name = random_name()
    response << random_name unless User.find(username: name)
  end while response.length < 100
  response
end

@MrJaba
Copy link

MrJaba commented Sep 23, 2014

def get_usernames(desired_names=100)
  names = (10*desired_names).times{ random_name }
  existing = User.all.pluck(:username)
  names = names - existing
  names_still_needed = desired_names - names.length
  if  names < desired_names
    names += get_usernames(names_still_needed)
  else
    names
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment