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.
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
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.
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 Dec 15, 11:48 pm, Daniel Peikes <danpei...@hotmail.com> wrote:
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.
--
Posted viahttp://www.ruby-forum.com/.
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"
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 16.12.2007 07:18, Daniel Peikes wrote:
Joseph Pecoraro 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"
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
···
On Dec 16, 4:27 am, Sebastian Hungerecker <sep...@googlemail.com> 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.