Ruby - Understanding Winning Method -


i have ruby code here has 2 series of hashes , there method.

valid_choices = {   'r' => 'rock',   'p' => 'paper',   'sc' => 'scissors',   'l' => 'lizard',   'sp' => 'spock' }  winning_combo = {   'r' => ['sc', 'l'],   'p' => ['r', 'sp'],   'sc' => ['p', 'l'],   'l' => ['sp', 'p'],   'sp' => ['sc', 'r'] }  def win?(first, second)   winning_combo[first].include?(second) end 

any 1 out there can explain me win? method does? , similar this?

def win?(first, second)   (first == 'sc' && second == 'p') ||     (first == 'p' && second == 'r') ||     (first == 'r' && second == 'l') ||     (first == 'l' && second == 'sp') ||     (first == 'sp' && second == 'sc') ||     (first == 'sc' && second == 'l') ||     (first == 'l' && second == 'p') ||     (first == 'p' && second == 'sp') ||     (first == 'sp' && second == 'r') ||     (first == 'r' && second == 'sc') end 

i hope can explanation in layman's term or in way dummy can understand method. sorry dummy here. need ideas. much! hope understand.

this first part winning_combo[first] looks 1 of entries in winning_combo. example if first "r" return array ['sc', 'l']:

winning_combo['r'] #=> ["sc", "l"] 

...we check if array contains value of second values "sc" or "l" true:

winning_combo['r'].include?('sc') #=> true  winning_combo['r'].include?('l') #=> true 

this equivalent longer win? method winning combinations listed out individual conditions.


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -