How t owrite an OR statement

D

Daniel Peikes

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.
 
J

Joseph Pecoraro

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
 
D

Daniel Peikes

Joseph said:
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
 
J

Joseph Pecoraro

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.

2) 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
 
S

Sebastian Hungerecker

Daniel said:
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
 
M

MonkeeSage

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.

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
 
S

Sebastian Hungerecker

MonkeeSage said:
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.
 
R

Robert Klemme

Joseph said:
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

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
 
M

MonkeeSage

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,274
Messages
2,571,366
Members
48,052
Latest member
EvaW192252

Latest Threads

Top