Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #4078 > unrolled thread

Help with while condition OR condition

Started by"Bill W." <sirwillard42@gmail.com>
First post2011-05-07 17:37 -0500
Last post2011-05-09 16:42 -0500
Articles 14 — 10 participants

Back to article view | Back to comp.lang.ruby


Contents

  Help with while condition OR condition "Bill W." <sirwillard42@gmail.com> - 2011-05-07 17:37 -0500
    Re: Help with while condition OR condition Steve Klabnik <steve@steveklabnik.com> - 2011-05-07 17:52 -0500
    Re: Help with while condition OR condition Christopher Dicely <cmdicely@gmail.com> - 2011-05-07 17:53 -0500
    Re: Help with while condition OR condition John Feminella <johnf@bitsbuilder.com> - 2011-05-07 17:54 -0500
      Re: Help with while condition OR condition John Feminella <johnf@bitsbuilder.com> - 2011-05-07 17:55 -0500
    Re: Help with while condition OR condition Joel VanderWerf <joelvanderwerf@gmail.com> - 2011-05-07 17:56 -0500
    Re: Help with while condition OR condition Hashmal <jeremypinat@gmail.com> - 2011-05-08 01:06 +0200
    Re: Help with while condition OR condition "Bill W." <sirwillard42@gmail.com> - 2011-05-07 18:38 -0500
    Re: Help with while condition OR condition Josh Cheek <josh.cheek@gmail.com> - 2011-05-07 20:15 -0500
    Re: Help with while condition OR condition 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-07 21:29 -0500
      Re: Help with while condition OR condition John Feminella <johnf@bitsbuilder.com> - 2011-05-07 21:48 -0500
      Re: Help with while condition OR condition Brian Candler <b.candler@pobox.com> - 2011-05-08 09:45 -0500
        Re: Help with while condition OR condition 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-09 15:11 -0500
          Re: Help with while condition OR condition Phillip Gawlowski <cmdjackryan@googlemail.com> - 2011-05-09 16:42 -0500

#4078 — Help with while condition OR condition

From"Bill W." <sirwillard42@gmail.com>
Date2011-05-07 17:37 -0500
SubjectHelp with while condition OR condition
Message-ID<fe6567119d5fbe466a7c6d2885d4718a@ruby-forum.com>
Hi everyone,

This is my first post, so I hope I don't sound too inexperienced..

I'm trying to teach myself Ruby, and have run into an issue with a while
statement that will break if an input is "exit" or "quit".
As of right now, it works if exit is input, but not quit

I know I am completely misusing the entire thing, but here is what I
came up with:

EXIT = "exit"  #need constants since Ruby gets pissed at string literals
QUIT = "quit"  #in a comparison

print "Input: "
input = gets
while input.chomp.downcase != (EXIT || QUIT) #only works for exit

  #Do something

print "Input: "  #pick up the next input and check it
input = gets
end

I know that Ruby has a lot of shortcuts, but if you post any please
explain how they work (or provide a link to a good explanation.

Thanks!

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [next] | [standalone]


#4079

FromSteve Klabnik <steve@steveklabnik.com>
Date2011-05-07 17:52 -0500
Message-ID<BANLkTin2Gbyeph3RJo3iV-eSnVOqd0iP=Q@mail.gmail.com>
In reply to#4078
[Note:  parts of this message were removed to make it a legal post.]

Don't do that, just use eql?

begin
  print "input: "
  input = gets.chomp.downcase
end while not (input.eql? "exit" or input.eql? "quit")

[toc] | [prev] | [next] | [standalone]


#4080

FromChristopher Dicely <cmdicely@gmail.com>
Date2011-05-07 17:53 -0500
Message-ID<BANLkTinVJp_zSaL37Y3NLFBofW4DtHhkfQ@mail.gmail.com>
In reply to#4078
On Saturday, May 7, 2011, Bill W. <sirwillard42@gmail.com> wrote:
> Hi everyone,
>
> This is my first post, so I hope I don't sound too inexperienced..
>
> I'm trying to teach myself Ruby, and have run into an issue with a while
> statement that will break if an input is "exit" or "quit".
> As of right now, it works if exit is input, but not quit
>
> I know I am completely misusing the entire thing, but here is what I
> came up with:
>
> EXIT = "exit"  #need constants since Ruby gets pissed at string literals
> QUIT = "quit"  #in a comparison
>
> print "Input: "
> input = gets
> while input.chomp.downcase != (EXIT || QUIT) #only works for exit

The || operator returns the operand on the left if it is "true-ish"
(anything other than nil or false), otherwise it evaluates and returns
the operand on the right. So (EXIT || QUIT) where EXIT="exit" just
evaluates to "exit".

You could do this with the || between two comparisons (rather than two
options in one comparison).

Also, Ruby has no problem with comparisons against string literals;
what made you think it did?

[toc] | [prev] | [next] | [standalone]


#4081

FromJohn Feminella <johnf@bitsbuilder.com>
Date2011-05-07 17:54 -0500
Message-ID<BANLkTintwqRe_=ewaAz29KU4DuXaRt8HvA@mail.gmail.com>
In reply to#4078
This line doesn't behave in the way I think you expect:

> while input.chomp.downcase != (EXIT || QUIT) #only works for exit

This says, "check if the downcased input is not equal to the value of
the expression `EXIT || QUIT`". What is the value of that expression?
In this case, it will resolve to EXIT, since the string "exit" is not
false or nil and is thus true. The value of QUIT is never evaluated.
So, if the input is not equal to exit, the while loop continues.

Why is that? In Ruby, all expressions have both a "value" and a
"truthiness". An expression is "falsy" if it evaluates to either
`false` or `nil`; otherwise it is `truthy`. In the case of an
expression like `foo || bar`, the truth table would look like this:

* foo is truthy, bar is truthy: result is `foo` and truthy
* foo is truthy, bar is falsy: result is `foo` and truthy
* foo is falsy, bar is truthy: result is `bar` and truthy
* foo is falsy, bar is falsy: result is `bar` and falsy

So you can see that in your case, foo is EXIT and bar is QUIT, both of
which are truthy values; thus the expression is "exit". To get what
you what want, try something like this:

    command = input.chomp.downcase
    while command != "exit" || command != "quit" ...

Or, more succinctly:

    case command
    when "exit", "quit"
      puts "exiting!"; ...
    else
      # do stuff
    end

~ jf
--
John Feminella
Principal Consultant, BitsBuilder
LI: http://www.linkedin.com/in/johnxf
SO: http://stackoverflow.com/users/75170/



On Sat, May 7, 2011 at 18:37, Bill W. <sirwillard42@gmail.com> wrote:
> Hi everyone,
>
> This is my first post, so I hope I don't sound too inexperienced..
>
> I'm trying to teach myself Ruby, and have run into an issue with a while
> statement that will break if an input is "exit" or "quit".
> As of right now, it works if exit is input, but not quit
>
> I know I am completely misusing the entire thing, but here is what I
> came up with:
>
> EXIT = "exit"  #need constants since Ruby gets pissed at string literals
> QUIT = "quit"  #in a comparison
>
> print "Input: "
> input = gets
> while input.chomp.downcase != (EXIT || QUIT) #only works for exit
>
>  #Do something
>
> print "Input: "  #pick up the next input and check it
> input = gets
> end
>
> I know that Ruby has a lot of shortcuts, but if you post any please
> explain how they work (or provide a link to a good explanation.
>
> Thanks!
>
> --
> Posted via http://www.ruby-forum.com/.
>
>

[toc] | [prev] | [next] | [standalone]


#4082

FromJohn Feminella <johnf@bitsbuilder.com>
Date2011-05-07 17:55 -0500
Message-ID<BANLkTik2_1XgXdvmaKcaM6YySN6BF9NsEg@mail.gmail.com>
In reply to#4081
Oops, small typo:

    while command != "exit" || command != "quit" ...

should be:

    while command != "exit" && command != "quit" ...
--
John Feminella
Principal Consultant, BitsBuilder
LI: http://www.linkedin.com/in/johnxf
SO: http://stackoverflow.com/users/75170/



On Sat, May 7, 2011 at 18:53, John Feminella <johnf@bitsbuilder.com> wrote:
> This line doesn't behave in the way I think you expect:
>
>> while input.chomp.downcase != (EXIT || QUIT) #only works for exit
>
> This says, "check if the downcased input is not equal to the value of
> the expression `EXIT || QUIT`". What is the value of that expression?
> In this case, it will resolve to EXIT, since the string "exit" is not
> false or nil and is thus true. The value of QUIT is never evaluated.
> So, if the input is not equal to exit, the while loop continues.
>
> Why is that? In Ruby, all expressions have both a "value" and a
> "truthiness". An expression is "falsy" if it evaluates to either
> `false` or `nil`; otherwise it is `truthy`. In the case of an
> expression like `foo || bar`, the truth table would look like this:
>
> * foo is truthy, bar is truthy: result is `foo` and truthy
> * foo is truthy, bar is falsy: result is `foo` and truthy
> * foo is falsy, bar is truthy: result is `bar` and truthy
> * foo is falsy, bar is falsy: result is `bar` and falsy
>
> So you can see that in your case, foo is EXIT and bar is QUIT, both of
> which are truthy values; thus the expression is "exit". To get what
> you what want, try something like this:
>
>    command = input.chomp.downcase
>    while command != "exit" || command != "quit" ...
>
> Or, more succinctly:
>
>    case command
>    when "exit", "quit"
>      puts "exiting!"; ...
>    else
>      # do stuff
>    end
>
> ~ jf
> --
> John Feminella
> Principal Consultant, BitsBuilder
> LI: http://www.linkedin.com/in/johnxf
> SO: http://stackoverflow.com/users/75170/
>
>
>
> On Sat, May 7, 2011 at 18:37, Bill W. <sirwillard42@gmail.com> wrote:
>> Hi everyone,
>>
>> This is my first post, so I hope I don't sound too inexperienced..
>>
>> I'm trying to teach myself Ruby, and have run into an issue with a while
>> statement that will break if an input is "exit" or "quit".
>> As of right now, it works if exit is input, but not quit
>>
>> I know I am completely misusing the entire thing, but here is what I
>> came up with:
>>
>> EXIT = "exit"  #need constants since Ruby gets pissed at string literals
>> QUIT = "quit"  #in a comparison
>>
>> print "Input: "
>> input = gets
>> while input.chomp.downcase != (EXIT || QUIT) #only works for exit
>>
>>  #Do something
>>
>> print "Input: "  #pick up the next input and check it
>> input = gets
>> end
>>
>> I know that Ruby has a lot of shortcuts, but if you post any please
>> explain how they work (or provide a link to a good explanation.
>>
>> Thanks!
>>
>> --
>> Posted via http://www.ruby-forum.com/.
>>
>>
>

[toc] | [prev] | [next] | [standalone]


#4083

FromJoel VanderWerf <joelvanderwerf@gmail.com>
Date2011-05-07 17:56 -0500
Message-ID<4DC5CE2A.9070402@gmail.com>
In reply to#4078
On 05/07/2011 03:37 PM, Bill W. wrote:
> Hi everyone,
>
> This is my first post, so I hope I don't sound too inexperienced..
>
> I'm trying to teach myself Ruby, and have run into an issue with a while
> statement that will break if an input is "exit" or "quit".
> As of right now, it works if exit is input, but not quit
>
> I know I am completely misusing the entire thing, but here is what I
> came up with:
>
> EXIT = "exit"  #need constants since Ruby gets pissed at string literals
> QUIT = "quit"  #in a comparison
>
> print "Input: "
> input = gets
> while input.chomp.downcase != (EXIT || QUIT) #only works for exit

It can help to take apart expressions in irb (interactive ruby):

$ irb
 >> EXIT = "exit"
=> "exit"
 >> QUIT = "quit"
=> "quit"
 >> EXIT || QUIT
=> "exit"
 >> "quit" == (EXIT || QUIT)
=> false

>    #Do something
>
> print "Input: "  #pick up the next input and check it
> input = gets
> end
>
> I know that Ruby has a lot of shortcuts, but if you post any please
> explain how they work (or provide a link to a good explanation.

Something to tinker with:

print "Input: "
while input = gets
   case s = input.chomp.downcase
   when "exit", "quit"
     puts "You wanted to #{s} this mighty fine program?"
     break
   else
     puts "Why do you say '#{s}'?"
     print "Input: "
   end
end
puts "Done."

[toc] | [prev] | [next] | [standalone]


#4084

FromHashmal <jeremypinat@gmail.com>
Date2011-05-08 01:06 +0200
Message-ID<iq4j9q$351$1@dont-email.me>
In reply to#4078
`(EXIT || QUIT)` will always return "exit" as EXIT is never `nil` or 
`false`, so in your case, the input string is never checked against `QUIT`.

Try this:

     print "Input: "
     input = gets

     until ["exit", "quit"].include? input.chomp.downcase
       # Do something
       print "Input: "
       input = gets
     end

As you can see you can do it without constants. the method `include?` 
checks if an element exists in the array. `until` is basically an 
inverted `while`.



On 5/8/11 12:37 AM, Bill W. wrote:
> Hi everyone,
>
> This is my first post, so I hope I don't sound too inexperienced..
>
> I'm trying to teach myself Ruby, and have run into an issue with a while
> statement that will break if an input is "exit" or "quit".
> As of right now, it works if exit is input, but not quit
>
> I know I am completely misusing the entire thing, but here is what I
> came up with:
>
> EXIT = "exit"  #need constants since Ruby gets pissed at string literals
> QUIT = "quit"  #in a comparison
>
> print "Input: "
> input = gets
> while input.chomp.downcase != (EXIT || QUIT) #only works for exit
>
>    #Do something
>
> print "Input: "  #pick up the next input and check it
> input = gets
> end
>
> I know that Ruby has a lot of shortcuts, but if you post any please
> explain how they work (or provide a link to a good explanation.
>
> Thanks!
>

[toc] | [prev] | [next] | [standalone]


#4088

From"Bill W." <sirwillard42@gmail.com>
Date2011-05-07 18:38 -0500
Message-ID<18481185cea836ccb36cbf1c5a566cf4@ruby-forum.com>
In reply to#4078
Wow!  I would say this is the most replies I have ever had on a 
programming topic!

Thanks for the in-depth description of how || works, I was WAY off.

I tried this with until already once, but I had the rest of it wrong and 
it failed.

command = input.chomp.downcase
until ["exit", "quit"].include? input.chomp.downcase
are both exactly the type of shortcuts i would have never though of!

Thanks again!

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#4095

FromJosh Cheek <josh.cheek@gmail.com>
Date2011-05-07 20:15 -0500
Message-ID<BANLkTimSPu+z7F6dE5pQyMBHeYE-jJHYsg@mail.gmail.com>
In reply to#4078
[Note:  parts of this message were removed to make it a legal post.]

On Sat, May 7, 2011 at 5:37 PM, Bill W. <sirwillard42@gmail.com> wrote:

>
> EXIT = "exit"  #need constants since Ruby gets pissed at string literals
> QUIT = "quit"  #in a comparison
>
>
It doesn't get pissed at string literals in comparison, its just that it was
able to tell that the way you were comparing didn't make sense (because
string literal is known at time you wrote it, not dynamically looked up, so
the comparison is also known, and doesn't make sens). So it was warning you
of the issue. It didn't complain when you stored then in vars/constants,
because it doesn't know their value until runtime. They could hypothetically
be false or nil, so it isn't conspicuously an error.

e.g.

if "some literal"
  # do something
end # !> string literal in condition

if "some literal" || "other"
  # do something
end # !> string literal in condition

[toc] | [prev] | [next] | [standalone]


#4101

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-05-07 21:29 -0500
Message-ID<21f342aaa24993413edad3ec1e101650@ruby-forum.com>
In reply to#4078
Hi,

A lot of beginners make the same mistake you did.   'Compound 
conditionals' have to be written like separate conditionals and then 
hooked together with an 'or' or 'and'.  For instance if you wanted to do 
something only if a number were greater than 5 and less then 10, you 
would do this:

x > 5
x < 10
and

if x > 5 and x < 10
  #do something
end

Everyone should use 'and' and 'or' by default instead of && and ||. 
Code reads better that way.  Only if you have a specific reason to, 
should you use && or ||.

Good luck.

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#4104

FromJohn Feminella <johnf@bitsbuilder.com>
Date2011-05-07 21:48 -0500
Message-ID<BANLkTikaY2NAr8EDK2DWUapnb4OLUu6AJA@mail.gmail.com>
In reply to#4101
> Everyone should use 'and' and 'or' by default instead of && and ||.
> Code reads better that way.  Only if you have a specific reason to,
> should you use && or ||.

It's not a good idea to make a blanket rule like that, imo. The "and"
keyword is not really a substitute for "&&", since it has different
precedence. It's best viewed as a control flow modifier (like "if" or
"unless" when at the end of an expression), rather than a true logical
operator.

If you don't know that it's not quite the same, this can get you into
big trouble by leading to subtle bugs. Consider this code, for
instance:

>> missiles_armed = true
 => true
>> go_for_launch = false
 => false

# Using &&
>> go_for_launch && missiles_armed ? :fire_ze_missiles : :abort_launch
 => :abort_launch # Looks good here.

# Using "and"
>> go_for_launch and missiles_armed ? :fire_ze_missiles : :abort_launch
 => false # Uh-oh! We didn't get the `:abort_launch` we were expecting...

~ jf
--
John Feminella
Principal Consultant, BitsBuilder
LI: http://www.linkedin.com/in/johnxf
SO: http://stackoverflow.com/users/75170/



On Sat, May 7, 2011 at 22:29, 7stud -- <bbxx789_05ss@yahoo.com> wrote:
> Hi,
>
> A lot of beginners make the same mistake you did.   'Compound
> conditionals' have to be written like separate conditionals and then
> hooked together with an 'or' or 'and'.  For instance if you wanted to do
> something only if a number were greater than 5 and less then 10, you
> would do this:
>
> x > 5
> x < 10
> and
>
> if x > 5 and x < 10
>  #do something
> end
>
> Everyone should use 'and' and 'or' by default instead of && and ||.
> Code reads better that way.  Only if you have a specific reason to,
> should you use && or ||.
>
> Good luck.
>
> --
> Posted via http://www.ruby-forum.com/.
>
>

[toc] | [prev] | [next] | [standalone]


#4130

FromBrian Candler <b.candler@pobox.com>
Date2011-05-08 09:45 -0500
Message-ID<f5103a314176f2296484e3603dc779f6@ruby-forum.com>
In reply to#4101
7stud -- wrote in post #997311:
> In ruby, everyone should use 'and' and 'or' by default instead of && and
> ||.

I would advise exactly the opposite: there are many traps for the unwary 
if you use 'and' and 'or'. Two prime examples:

>> val = 10
=> 10
>> ok = val < 3 or val > 5
=> true
>> ok
=> false

>> a = true
=> true
>> b = not a
SyntaxError: compile error
(irb):7: syntax error, unexpected kNOT

Use '||' and '!' respectively and you won't have a problem.

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#4175

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-05-09 15:11 -0500
Message-ID<3fa895154703338997c23d7a582c2098@ruby-forum.com>
In reply to#4130
Brian Candler wrote in post #997379:
>
> Use '||' and '!' respectively and you won't have a problem.
>

lol.

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#4176

FromPhillip Gawlowski <cmdjackryan@googlemail.com>
Date2011-05-09 16:42 -0500
Message-ID<BANLkTikU=ufG7mbVT=UNk7afKPD3dLejDA@mail.gmail.com>
In reply to#4175
On Mon, May 9, 2011 at 10:11 PM, 7stud -- <bbxx789_05ss@yahoo.com> wrote:
> Brian Candler wrote in post #997379:
>>
>> Use '||' and '!' respectively and you won't have a problem.
>>
>
> lol.

Should've tried the code first:

irb(main):001:0> val = 10
=> 10
irb(main):002:0> ok = val < 3 || val > 5
=> true
irb(main):003:0> ok
=> true
irb(main):004:0> a = true
=> true
irb(main):005:0> b = !a
=> false

Using *only* "or" or "not" obviously doesn't lead to the expected
results, while using || and ! do.

-- 
Phillip Gawlowski

Though the folk I have met,
(Ah, how soon!) they forget
When I've moved on to some other place,
There may be one or two,
When I've played and passed through,
Who'll remember my song or my face.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.ruby


csiph-web