# How t owrite an OR statement

**URL:** <https://www.ruby-forum.com/t/how-t-owrite-an-or-statement/123306>\
**Category:** Ruby\
**Created:** [December 16, 2007, 6:48am UTC](https://www.ruby-forum.com/t/how-t-owrite-an-or-statement/123306 "2007-12-16T06:48:39Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dannypotta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a9adbd/32.png) [@dannypotta](https://www.ruby-forum.com/u/dannypotta)\
**Post date:** [December 16, 2007, 6:48am UTC](https://www.ruby-forum.com/t/how-t-owrite-an-or-statement/123306/1 "2007-12-16T06:48:39Z")

</div>

Ok folks I know this is an easy question, I am trying to write a while  
loop with a logical or statement and am new to ruby. The online guide I  
am using does not seem to cover and and or statements for some reason.  
My guess being a C programmer is to do it like this:  
while again != “y” || “n”

I am looking for confirmation, also suggestions of a better online guide  
to learn Ruby preffrubly with project to do that include instructions.  
Thanks in advance.

---

<div class="post-metadata">

**Author:** ![dannypotta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a9adbd/32.png) [@dannypotta](https://www.ruby-forum.com/u/dannypotta)\
**Post date:** [December 16, 2007, 7:12am UTC](https://www.ruby-forum.com/t/how-t-owrite-an-or-statement/123306/2 "2007-12-16T07:12:33Z")

</div>

> while again != “y” || “n”

These don’t use the OR logic, but they are similiar.  
until again ~= /^[yn]/i do  
…  
end

Or if your wanted to keep the while keyword you could do this as:  
while again[0] != /^[^yn]/i do  
…  
end

I don’t know if you can do exactly what you are trying to do with that  
syntax. You can reconstruct the logic like this:  
while again != “y” && again != “n” do  
…  
end

Here is another approach more along the lines of what you have (is  
something in a list of something else), likewise the until loop could be  
made a while loop:  
good\_chars = %w(y n) #=\> [‘y’, ‘n’]  
until good\_chars.include? again do  
…  
end

---

<div class="post-metadata">

**Author:** ![dannypotta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a9adbd/32.png) [@dannypotta](https://www.ruby-forum.com/u/dannypotta)\
**Post date:** [December 16, 2007, 7:18am UTC](https://www.ruby-forum.com/t/how-t-owrite-an-or-statement/123306/3 "2007-12-16T07:18:36Z")

</div>

Joseph P. wrote:

> > while again != “y” || “n”
> 
> These don’t use the OR logic, but they are similiar.  
> until again ~= /^[yn]/i do  
> …  
> end
> 
> Or if your wanted to keep the while keyword you could do this as:  
> while again[0] != /^[^yn]/i do  
> …  
> end
> 
> I don’t know if you can do exactly what you are trying to do with that  
> syntax. You can reconstruct the logic like this:  
> while again != “y” && again != “n” do  
> …  
> end
> 
> Here is another approach more along the lines of what you have (is  
> something in a list of something else), likewise the until loop could be  
> made a while loop:  
> good\_chars = %w(y n) #=\> [‘y’, ‘n’]  
> until good\_chars.include? again do  
> …  
> end

This is what I currently have, but it looks to be giving me an infinite  
loop:  
again = “y”  
number = []

while again == “y”

puts “Please enter a number.”

number = gets.chomp

puts “Would you like to enter a number again? y/n”

again = gets.chomp.downcase

while again != “y” or “n”

```
puts "Would you like to enter a number again? y/n"

```

end

end

puts number

hold = gets

---

<div class="post-metadata">

**Author:** ![dannypotta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a9adbd/32.png) [@dannypotta](https://www.ruby-forum.com/u/dannypotta)\
**Post date:** [December 16, 2007, 10:30am UTC](https://www.ruby-forum.com/t/how-t-owrite-an-or-statement/123306/4 "2007-12-16T10:30:44Z")

</div>

Daniel P. wrote:

> My guess being a C programmer is to do it like this:  
> while again != “y” || “n”

Maybe my C is a bit rusty, but isn’t anything that’s not zero true in C?  
If so  
that would be an infinite loop (it would certainly be one in ruby).  
You’re  
basically saying “while something that might be true or not or  
something  
that’s always true” and since “true or true” as well as “false or true”  
are  
true, this always evaluates to true and thus leads to an infinite loop.

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![dannypotta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a9adbd/32.png) [@dannypotta](https://www.ruby-forum.com/u/dannypotta)\
**Post date:** [December 16, 2007, 7:26am UTC](https://www.ruby-forum.com/t/how-t-owrite-an-or-statement/123306/5 "2007-12-16T07:26:17Z")

</div>

> while again != “y” or “n”
> 
> ```
> puts "Would you like to enter a number again? y/n"
> 
> ```
> 
> end

1. The logic here has two parts, two sides of the or.  
again != “y” #=\> false  
“n” #=\> true because “n” his is not nil

So “false or true” will always be true.

1. Inside your while loop you never let the user reinput. You need to  
add:  
again = gets.chomp.downcase

Any of my above suggestions should work for the logic portion. Here is  
an example, replace the above three lines with these four:

until again == “y” or again == “n” do  
puts “Would you like to enter a number again? y/n”  
again = gets.chomp.downcase  
end

---

<div class="post-metadata">

**Author:** ![dannypotta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a9adbd/32.png) [@dannypotta](https://www.ruby-forum.com/u/dannypotta)\
**Post date:** [December 16, 2007, 11:29am UTC](https://www.ruby-forum.com/t/how-t-owrite-an-or-statement/123306/6 "2007-12-16T11:29:51Z")

</div>

MonkeeSage wrote:

> Also, again != “y” || “n” evaluates to ((again != “y”) == true) ||  
> (“n” == true)

Not quite.  
“n” == true would always be false while “n” will always be true.

---

<div class="post-metadata">

**Author:** ![dannypotta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a9adbd/32.png) [@dannypotta](https://www.ruby-forum.com/u/dannypotta)\
**Post date:** [December 16, 2007, 11:21am UTC](https://www.ruby-forum.com/t/how-t-owrite-an-or-statement/123306/7 "2007-12-16T11:21:04Z")

</div>

On Dec 15, 11:48 pm, Daniel P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Posted viahttp://www.ruby-forum.com/.  
> Also, again != “y” || “n” evaluates to ((again != “y”) == true) ||  
> (“n” == true)…

puts “oops” if (“b” != “b” || “a”)

# =\> oops

…the “b” != “b” branch is false, but then we just get a string on  
the other branch (rather than the condition “b” != “a”), and since “a”  
is not nil/false is evaluates as true (though you’ll get a warning  
about using a string literal as a condition). If you want to test a  
condition on different branches, you have to specify the testee on  
each one…

again == “y” || again == “n” || again == “maybe”

…or use a short-cut…

[“y”, “n”, “maybe”].include?(again)

Regards,  
Jordan

---

<div class="post-metadata">

**Author:** ![dannypotta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a9adbd/32.png) [@dannypotta](https://www.ruby-forum.com/u/dannypotta)\
**Post date:** [December 16, 2007, 1:55pm UTC](https://www.ruby-forum.com/t/how-t-owrite-an-or-statement/123306/8 "2007-12-16T13:55:48Z")

</div>

On 16.12.2007 07:18, Daniel P. wrote:

> > …  
> > Here is another approach more along the lines of what you have (is  
> > number = []  
> > again = gets.chomp.downcase
> 
> hold = gets

Your logic will become easier if you use post checked loops:

numbers = []  
again = nil

begin  
puts “Please enter a number.”  
numbers \<\< Integer(gets)

begin  
puts “Would you like to enter a number again? y/n”  
again = gets.chomp.downcase  
end until again == “y” || again == “n”  
end while again == “y”

puts numbers

With regard to “or”: “or” has a much lower precedence than “||” so it is  
usually better in conditions like these to use “||”.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![dannypotta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a9adbd/32.png) [@dannypotta](https://www.ruby-forum.com/u/dannypotta)\
**Post date:** [December 16, 2007, 2:30pm UTC](https://www.ruby-forum.com/t/how-t-owrite-an-or-statement/123306/9 "2007-12-16T14:30:55Z")

</div>

On Dec 16, 4:27 am, Sebastian H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> MonkeeSage wrote:
> 
> > Also, again != “y” || “n” evaluates to ((again != “y”) == true) ||  
> > (“n” == true)
> 
> Not quite.  
> “n” == true would always be false while “n” will always be true.
> 
> –  
> Jabber: removed\_email\_address@domain.invalid  
> ICQ: 205544826

Yeah, I was just trying to give an idea of how it’s evaluated, not  
give interchangeable ruby code (that’s why I said a little after  
‘…since “a” is not nil/false is evaluates as true…’). But thank  
you for clarifying; I wasn’t being very clear there.

Regards,  
Jordan
