How t owrite an OR statement

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.

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

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

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

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

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.

On Dec 15, 11:48 pm, Daniel P. [email protected] 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

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

On Dec 16, 4:27 am, Sebastian H. [email protected]
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: [email protected]
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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs