[#401849] If statement — Masoud Ahmadi <lists@...>

Will anyone be able to point out what I am doing wrong.

15 messages 2012/12/02

[#401987] Trying to get "translator" to work — JD KF <lists@...>

So, basically, I'm trying to get the below code to work properly for

12 messages 2012/12/06

[#402012] Need help to select some listbox item in different listbox together — Jonathan Masato <lists@...>

Hello,

10 messages 2012/12/07

[#402045] if n belongs to set a and m belongs to set b repeat some steps, How? — "zubair a." <lists@...>

We can do so in java and similar languages like:

11 messages 2012/12/08

[#402078] Time.new(2001, 12, 3).to_i returns wrong value — Robert Buck <lists@...>

I am doing something that not many do, I am writing a database driver

9 messages 2012/12/09

[#402145] How I can create/extract a variable/hash into the current binding in Ruby? — Ramon de C Valle <rcvalle@...>

Hi,

12 messages 2012/12/12

[#402205] Wondering About Flatiron School — "Kevin Y." <lists@...>

Hi everyone!,

35 messages 2012/12/15
[#402207] Re: Wondering About Flatiron School — Chad Perrin <code@...> 2012/12/15

On Sat, Dec 15, 2012 at 11:51:08AM +0900, Kevin Y. wrote:

[#402214] Ruby quick reference arranged in ASCII sequence? — Old Grantonian <lists@...>

As a ruby beginner, I would be grateful for any links to a ruby

17 messages 2012/12/15

[#402226] print - and strip text between tags using Nokogiri — Paul Mena <lists@...>

I'm a Ruby Newbie trying to write a program to process thousands of HTML

13 messages 2012/12/15

[#402332] Perl to Ruby: regex captures to assignment. — "Derrick B." <lists@...>

Hello all,

37 messages 2012/12/19
[#402342] Re: Perl to Ruby: regex captures to assignment. — "Derrick B." <lists@...> 2012/12/20

First of all, thanks for the fast responses!

[#402352] Re: Perl to Ruby: regex captures to assignment. — Robert Klemme <shortcutter@...> 2012/12/20

On Thu, Dec 20, 2012 at 1:38 AM, Derrick B. <lists@ruby-forum.com> wrote:

[#402357] Re: Perl to Ruby: regex captures to assignment. — "Derrick B." <lists@...> 2012/12/20

Robert Klemme wrote in post #1089733:

[#402359] trying to strip characters from a line — Paul Mena <lists@...>

I'm reading a table from a MySQL database and then processing it row by

18 messages 2012/12/20

[#402394] simple division: -9 / 5 = -2 what? — "Derrick B." <lists@...>

$ irb

13 messages 2012/12/22

[#402412] POLS and string-handling — Paul Magnussen <lists@...>

Hi,

14 messages 2012/12/22

[#402460] "Open" dialog of Windows — "Damián M. González" <lists@...>

Hi guys, been researching about pop up the "open" file dialog of

11 messages 2012/12/24

[#402466] How do I install Ruby on my Ubuntu 12.10 partition. — Kaye Ng <lists@...>

I already have Ruby installed on my Windows 7 partition.

23 messages 2012/12/25

[#402510] Ruby Association Certified Ruby Programmer — Sean Westfall <lists@...>

How well respected is this certification in the industry: Ruby

27 messages 2012/12/27
[#402528] Re: Ruby Association Certified Ruby Programmer — Peter Hickman <peterhickman386@...> 2012/12/27

On 27 December 2012 01:28, Sean Westfall <lists@ruby-forum.com> wrote:

[#402555] numeric? — Brandon Weaver <keystonelemur@...>

I've found a bit of an annoyance trying to find out if a number is numeric

20 messages 2012/12/27

[#402580] Ruby Koans regarding Hashes. — "Derrick B." <lists@...>

I am trying to understand this, so let me know how I do. :) I know

18 messages 2012/12/28

[#402609] can't open new ruby program under "new" context menu — "Lee V." <lists@...>

I'm stuck on the new version at trying to do something very simple.

10 messages 2012/12/28

[#402642] require "test/unit" — "Mattias A." <lists@...>

Hi,

17 messages 2012/12/29
[#402667] Re: require "test/unit" — "Mattias A." <lists@...> 2012/12/31

Hi Dami=C3=A1n M. Gonz=C3=A1lez!

[#402747] Re: require "test/unit" — "Derrick B." <lists@...> 2013/01/04

Mattias A. wrote in post #1090700:

[#402749] Re: require "test/unit" — sto.mar@... 2013/01/04

Am 04.01.2013 19:48, schrieb Derrick B.:

Re: String literal in condition and Invalid retry error in code.

From: Jes俍 Gabriel y Gal疣 <jgabrielygalan@...>
Date: 2012-12-05 08:59:32 UTC
List: ruby-talk #401931
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.

In This Thread