4

This might be a silly question, but I just can't get it to work. Pretty sure I've missed something.

I want to set a boolean to false Then set it to true only when a condition is met.

boolTest = false

until boolTest = true
    puts "Enter one 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 just starting to learn Ruby, so maybe I'm confusing the capabilities of other languages.

4
  • boolTest = true, don't add quotes. Commented Jul 15, 2015 at 8:37
  • 1
    Also, according to ruby conventions, should be bool_test (snake_case), not boolTest (camelCase). Commented Jul 15, 2015 at 8:37
  • apologies I copied the wrong code I didnt have quote in the original Commented Jul 15, 2015 at 8:38
  • 1
    here's another error. until boolTest == true Commented Jul 15, 2015 at 8:38

2 Answers 2

9

Since you're using until, that's effectively writing out while not boolTest. You can't use =, since that's reserved for assignment; instead, omit the boolean conditional. There's no value in checking a boolean against a boolean; if you really wanted to keep it though, you'd have to use ==.

boolTest = false

until boolTest
  puts "Enter one 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 an optimization/readability tip, you can also adjust your boolean conditional so that there's no repeated statement with choice; you can declare all of thoe strings in an array, and check to see if choice exists in the array through include?.

boolTest = false

until boolTest
  puts "Enter one fo these choices: add / update / display / delete?"
  choice = gets.chomp.downcase

  boolTest = %w(add update display delete).include? choice
end
Sign up to request clarification or add additional context in comments.

3 Comments

Your answer is now invalid :)
ah so I cannot write until boolTest = True - because now it seems to work
@cocojay = is an assignment, you have to use ==
1

I think what you missed is just "==" in the if condition until boolTest = true you should use a double = not a single one

this will work for you

boolTest = false

until boolTest == true
  puts "Enter one fo these choices: add / update / display / delete?"
  choice = gets.chomp.downcase

  if choice == "add" || choice == "update" || choice == "display" || choice == "delete"
    boolTest = true
  end
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.