Question about constants (Beginner)

First post on the site, if there’s a better place for these kinds of
questions please let me know and I’ll post there. When I try to run this
bit of code ruby gives me “dynamic constant assignment” syntax error in
the piece of code I uploaded. (couldn’t figure out how to format it for
the post.

As I’m writing this I’m thinking a possible reason may be that I’m
attempting to call a method for the Array class on a constant, but
CONSTANT.class -> Array so I don’t see why that would cause a problem.
I’m aware while loops are
not often times best practice: I’m learning how to program for the first
time and am exploring how code works. Thanks for the help!

Examine the output from this program:

def bar
choices = [1, 2, 3]
puts choices.object_id

input = gets.to_i
until choices.include?(input) == true
puts “we’re in the loop”
input = gets.to_i
end
puts ‘out of loop’
end

bar
bar

–output:–
2152343160
10
we’re in the loop
1
out of loop
2152342960
2
out of loop

There are two different object_id’s in the output, which means the
program assigned two different arrays to the variable choices. In this
case, there’s no error because choices is not a constant. “But, but,
but I didn’t run my def twice, so there was no reassignment to the
constant!” It doesn’t matter, the potential was there, so it’s not
allowed. The solution is to move the constant outside the def.

Also note, the statement: choices.include?(input) will return true
or false. Say, it returns true. Then your if statement becomes: if
true == true. Now suppose the conditional returns false and your if
statement becomes if false == true. Note that the result of evaluating
both those == statements is just the left hand side, i.e. true in the
first case and false in the second case. As a result, you can just
write:

until choices.include?(input)

That is actually more efficient because it does not make ruby
do the == test and produce another (boolean)result.

There are some schools of thought that believe “until” is an abomination
because it’s too hard to figure out what the result is–it’s sort of
like a double negative brain bender. I attend those schools. Everyone
else here does not.