ruby - Rails iteration with some smarts -


i'm putting little game people voting see 2 other people match based on interests they've entered , i'm having trouble getting code want. have data "friends" , i'd following:

  1. select 1 of "friends"
  2. find match of friend
  3. if can't find friends' matches voted on, random match

i'd steps 1 , 2 random don't see friend each time. recording votes now, have list of votes already. have far, can't figure out how put 2 together.

found_new_match = false  #try connected users first # connected_users = current_user.get_connected_users connected_users = []  unless connected_users.blank?   user = connected_users.shuffle.first   @match = user.matches.first end  # here i'd detect whether got through our connections' matches while found_new_match == false    found_new_match = true if @match = current_user.get_random_new_match end 

assuming supporting logic of code (getting connected users) works correctly , question how collapse logic single line, following should work fine:

  @match = current_user.get_connected_users.shuffle.first.try(:matches).try(:first) || current_user.get_random_new_match 

if factored "first match of user" (user.matches.first) own accessor method, skip terrible looking .try(:matches).try(:first). further factor "first random match" convenience method on user, such code shorten further into:

  @match = current_user.get_random_connected_match || current_user.get_random_new_match 

assuming "get_random_connected_match" act of getting connected users, shuffling out option, , extracting first match that. repeat refactoring ad nauseum.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -