[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How t owrite an OR statement

Daniel Peikes

12/16/2007 5:49:00 AM

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 via http://www.ruby-....

8 Answers

Joseph Pecoraro

12/16/2007 6:13:00 AM

0

> 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
--
Posted via http://www.ruby-....

Daniel Peikes

12/16/2007 6:19:00 AM

0

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"

end

end

puts number

hold = gets


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

Joseph Pecoraro

12/16/2007 6:26:00 AM

0

> 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

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

Sebastian Hungerecker

12/16/2007 9:30:00 AM

0

Daniel Peikes 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
--
Jabber: sepp2k@jabber.org
ICQ: 205544826

MonkeeSage

12/16/2007 10:16:00 AM

0

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

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

Sebastian Hungerecker

12/16/2007 10:27:00 AM

0

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: sepp2k@jabber.org
ICQ: 205544826

Robert Klemme

12/16/2007 12:53:00 PM

0

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"
>
> 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

MonkeeSage

12/16/2007 1:26:00 PM

0

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.
>
> --
> Jabber: sep...@jabber.org
> 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