Mistake with decision making in Ruby -


i'm stuck @ part of decision making. when answer variable n (when type n means don't want print text in different lines) , marks variable y (when type y means don't want print text quotation marks), i'm getting message: answer 'y' invalid. program terminating. here code:

def multifunctionalprinter      puts "how many times want print?"     number = gets     puts "-------------------------------------------"     puts "what want print?"     text = gets.chomp                                                    #chomping variable format decision making     puts "-------------------------------------------"     puts "do want print in different lines (y or n)?"     answer = gets.chomp                                                  #chomping variable format decision making     puts "-------------------------------------------"     puts "do want add quotation marks (') (y or n)?"     marks = gets.chomp                                                   #chomping variable format decision making      if answer == "y" && marks == "y"                                     # different lines: on , quotation marks: on         1.upto(number.to_i) {puts "'" + text.to_s + "'"}     elsif answer == "y" && marks == "n"                                  # different lines: on , quotation marks: off         1.upto(number.to_i) {puts text.to_s}     elsif answer == "y" && marks != "y" || marks != "n"                  # different lines: on , quotation marks: invalid         puts "answer '#{marks}' invalid. program terminating."     elsif answer == "n" && marks == "y"                                  # different lines: off , quotation marks: on         puts "do want split print? (y or n)"         answer2 = gets.chomp         if answer2 == "y"                                                # split: on              1.upto(number.to_i) {print "'" + text.to_s + "' "}             print "\n"         elsif answer2 == "n"                                             # split: off             1.upto(number.to_i) {print "'" + text.to_s + "'"}         else             puts "answer '#{answer2}' invalid. program terminating."         end      elsif answer == "n" && marks == "n"                                  # different lines: off , quotation marks: off         puts "do want split print? (y or n)"         answer2 = gets.chomp         if answer2 == "y"                                                # split: on             1.upto(number.to_i) {print text.to_s + " "}         elsif answer2 == "n"                                             # split: off             1.upto(number.to_i) {print text.to_s}         else             puts "answer '#{answer}' invalid. program terminating."         end      else         puts "answer '#{answer}' or '#{marks}' invalid. program terminating."     end      puts "--------------------------------------------" end   multifunctionalprinter 

what doing wrong?

the binary operator && has higher precedence ||.
means line:

answer == "y" && marks != "y" || marks != "n" 

is equivalent to

(answer == "y" && marks != "y") || marks != "n" 

so whenever marks different "n", statement holds true.


what should to solve problem adding bunch of parentheses.

answer == "y" && (marks != "y" || marks != "n") 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -