How to return the program flow at the beginning in Ruby -


i have player class in ruby solution. in class can define 2 players. need make validation , if names of players same, need throw message , in case same, need return program beginning. below code:

class player   print "first player, put name press enter: "   name1 = gets.chomp   print "second player, put name press enter: "   name2 = gets.chomp    if name1.eql?(name2)     abort "put different name players!"   end  end 

you need loop

name1 = name2 = nil while name1 == name2   print "first player, put name press enter: "   name1 = gets.chomp   print "second player, put name press enter: "   name2 = gets.chomp    if name1 == name2     print "put different name players!"   end end 

you can place object, is, class has no sense @ all. defined class, put code body of class without wrapping method.

it should be

class player   def play     name1 = name2 = nil     while name1 == name2       print "first player, put name press enter: "       name1 = gets.chomp       print "second player, put name press enter: "       name2 = gets.chomp        if name1 == name2         print "put different name players!"       end     end   end end 

and can call without

player = player.new player.play 

Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -