Set boolean variable in Ruby -


this might silly question, can't work. pretty sure i've missed something.

i want set boolean false set true when condition met.

booltest = false  until booltest = true     puts "enter 1 fo these choices: add / update / display / delete?"     choice = gets.chomp.downcase      if choice == "add" || choice == "update" || choice == "display" || choice == "delete"         booltest = true     end end 

only starting learn ruby, maybe i'm confusing capabilities of other languages.

since you're using until, that's writing out while not booltest. can't use =, since that's reserved assignment; instead, omit boolean conditional. there's no value in checking boolean against boolean; if wanted keep though, you'd have use ==.

booltest = false  until booltest   puts "enter 1 fo these choices: add / update / display / delete?"   choice = gets.chomp.downcase    if choice == "add" || choice == "update" || choice == "display" || choice == "delete"     booltest = true   end end 

as optimization/readability tip, can adjust boolean conditional there's no repeated statement choice; can declare of thoe strings in array, , check see if choice exists in array through include?.

booltest = false  until booltest   puts "enter 1 fo these choices: add / update / display / delete?"   choice = gets.chomp.downcase    booltest = %w(add update display delete).include? choice end 

Comments