On Wed, Dec 5, 2012 at 7:27 AM, Nuggety Nanna <lists / ruby-forum.com> wrote:
> I am attempting to make a Vector Calculator, that will take either
> coordinates, or an already given vector. The if and elsif blocks work
> fine, it is the else block that I am in need of help in. I want the code
> to re-run from the "answer = gets.chomp.to_i" line if the answer does
> not equate to "given" or "coordinates", but when i run the code I am
> given the following errors:
>
> vector_calc.rb:48: warning: string literal in condition
> vector_calc.rb:51: warning: string literal in condition
> vector_calc.rb:50: Invalid retry
> vector_calc.rb: compile error (SyntaxError)
>
> The code is below.
>
> puts "Are the vectors given, or do you only know coordinates?"
> puts "Please answer either 'given' or 'coordinates'."
>
> begin
> answer = gets.chomp.downcase
>
>   if answer == "coordinates"
>     puts "What is the x-value of your first coordinate?"
>     x1 = gets.chomp.to_i
>
>     puts "What is the y-value of your first coordinate?"
>     y1 = gets.chomp.to_i
>
>     puts "What is the x-value of your second coordinate?"
>     x2 = gets.chomp.to_i
>
>     puts "What is the y-value of your second coordinate?"
>     y2 = gets.chomp.to_i
>
>     coord_1 = [x1, y1]
>     coord_2 = [x2, y2]
>
>     puts "Point A = (#{x1}, #{y1})"
>     puts "Point B = (#{x2}, #{y2})"
>
>   elsif answer == "given"
>     puts "What is vector A's x-value?"
>     vect_Ax = gets.chomp.to_i
>
>     puts "what is vector A's y-value?"
>     vect_Ay = gets.chomp.to_i
>
>     vect_A = [vect_Ax, vect_Ay]
>
>     puts "What is vector B's x-value?"
>     vect_Bx = gets.chomp.to_i
>
>     puts "what is vector B's y-value?"
>     vect_By = gets.chomp.to_i
>
>     vect_B = [vect_Bx, vect_By]
>
>     puts "Vector A = [#{vect_Ax}, #{vect_Ay}]"
>     puts "Vector B = [#{vect_Bx}, #{vect_By}]"
>   else
>       if answer != "given" || "coordinates"
>         then puts "Please answer either 'given' or 'coordinates'."
>       end
>     until answer == "coordinates" || "given"
>       retry
>     end
>   end
> end

The conditions are wrong:

1.9.2p290 :001 > answer = "x"
 => "x"
1.9.2p290 :002 > if answer != "coordinates" || "given"
1.9.2p290 :003?>   puts "a"
1.9.2p290 :004?>   end
(irb):4: warning: string literal in condition
a

A string literal will always evaluate to true in a boolean comparison:

if answer != "coordinates" || answer != "given"

Now, the || (or) is wrong, because any word is either != "coordinates"
or != "give". You want && (and):

if answer != "coordinates" && answer != "given"

But, as you have this comparison in an else of an if that is comparing
answer to coordinates and given, it's not even needed.

Anyway, I think it would be simpler if you moved this logic to the
beggining of your code:

begin
 puts "Please enter coordinates or given"
  answer = gets.chomp.downcase
end until answer == "coordinates" || answer == "given"
# at this point you know that answer is either coordinates or given
if answer == "coordinates"
[...]

> P.S I am creating this code as part of an outcome to a research project,
> and am asking your permission to use your answer to this question as
> evidence to my folio.

NP.

Jesus.