Building a method - Learn to Program Section 9.5

In this program

def ask question
  while true
    puts question
    reply = gets.chomp.downcase

    if (reply != 'yes' || reply != 'no')
      puts 'Please answer "yes" or "no".'
    elsif reply == ''
    break
    end
  end
end

puts 'Hello, and thank you for...'
puts

ask "Do you like eating tacos?" # Ignore this return value
ask "Do you like eating burritos?" # And this one
wets_bed = ask 'Do you wet the bed?' # Save this return value
ask 'Do you like eating chimichangas?'
ask 'Do you like eating sopapillas?'
puts 'Just a few more questions...'
ask 'Do you like drinking horchata?'
ask 'Do you like eating flautas?'

puts
puts 'DEBRIEFING:'
puts 'Thank you for...'
puts
puts wets_bed

am I stuck in a loop where no matter whether I enter 'yes' or 'no' or
anything at all after I'm asked

···

---
Hello, and thank you for...

Do you like eating tacos?
---

I get back

---
Please answer "yes" or "no".
Do you like eating tacos?
---

because I decided to ask if reply is different than 'yes' and/or
different than 'no'? as well as ask if it's the same as an empty string,
basically saying that this is a situation that will always be true?

I got this program to work by rewriting it

def ask question
  while true
    puts question
    reply = gets.chomp.downcase

if (reply == 'yes' || reply == 'no')
      break
    else
      puts 'Please answer "yes" or "no".'
       if reply == ''
      break
       end
    end

etc etc....

, but I want to understand what I'd need to do to get it to work with !=
method.

Any insight / help would be appreciated.

Thanks,

Emeka

PS
I know since I didn't save a value for wets_bed I'm going to get nil
back, but just want to figure out the other stuff first.

---------

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

, but I want to understand what I'd need to do to get it to work with !=
method.

Any insight / help would be appreciated.

Thanks,

Emeka

if (reply != 'yes' || reply != 'no')

What happens if you type 'yes' ?
Then reply != 'no' so this is true.

What happens if you type 'no' ?
Then reply != 'yes' so this is true.

What happens if you type 'foo' ?
Then reply != 'yes' so this is true.

Try if (reply != 'yes' && reply != 'no')

Harry

wrote (rearranged a bit for illustrative purposes):

I got this program to work by rewriting it

[from]

while true
puts question
reply = gets.chomp.downcase

if (reply != 'yes' || reply != 'no')
puts 'Please answer "yes" or "no".'
elsif reply == ''
break
end
end

[to]

while true
puts question
reply = gets.chomp.downcase

if (reply == 'yes' || reply == 'no')
break
else
puts 'Please answer "yes" or "no".'
if reply == ''
break
end
end

The trick is that || means "or". The first time, you're saying "if
the reply isn't yes, or it isn't no, ask again, else if it's blank,
break". No matter what the reply is, it's either going to be not yes,
or not no. The second time, you're saying 'if it IS yes, or it IS no,
break, else ask again (and if it's blank, break)". If you want to use
!=, what you need there is && (and) rather than || (or).

If it helps, you can actually use the words "and" and "or" instead of
the || and &&. There are some situations in which there are
differences, but in this case it should be OK.

A general technique that might help is "pseudocoding". Write out
first, what it would look like in plain English (or whatever your
native language is) if you were giving instructions to a person -- a
fairly simple-minded person who is excellent at following instructions
exactly as given (no matter how wrong they may be), very quickly.
Read that over, trying to "think" like such a person. Maybe even
simulate it step by step by writing down the values of variables.
When that gives you the results you want, then start translating it
from English to Ruby. That's how most people start out learning to
program. When you get good at it, then you can skip the English step.

-Dave

···

On Fri, Dec 9, 2011 at 19:14, Emeka Patrick <emekapatrick@gmail.com>

--
LOOKING FOR WORK! What: Ruby (on/off Rails), Python, other modern languages.
Where: Northern Virginia, Washington DC (near Orange Line), and remote work.
See: davearonson.com (main) * codosaur.us (code) * dare2xl.com (excellence).
Specialization is for insects. (Heinlein) - Have Pun, Will Babble! (Aronson)

Hi Dave,

Thanks for the feedback and help! Definitely clarifies this. Didn't
cover && as of yet in the book so wasn't aware of using it as an option.

I'll definitely try pseudocoding, think it could really help me think
through some of this stuff.

Much appreciated.

-Emeka

Dave Aronson wrote in post #1036048:

···

On Fri, Dec 9, 2011 at 19:14, Emeka Patrick <emekapatrick@gmail.com>
wrote (rearranged a bit for illustrative purposes):

I got this program to work by rewriting it

[from]

while true
puts question
reply = gets.chomp.downcase

if (reply != 'yes' || reply != 'no')
puts 'Please answer "yes" or "no".'
elsif reply == ''
break
end
end

[to]

  end

end

The trick is that || means "or". The first time, you're saying "if
the reply isn't yes, or it isn't no, ask again, else if it's blank,
break". No matter what the reply is, it's either going to be not yes,
or not no. The second time, you're saying 'if it IS yes, or it IS no,
break, else ask again (and if it's blank, break)". If you want to use
!=, what you need there is && (and) rather than || (or).

If it helps, you can actually use the words "and" and "or" instead of
the || and &&. There are some situations in which there are
differences, but in this case it should be OK.

A general technique that might help is "pseudocoding". Write out
first, what it would look like in plain English (or whatever your
native language is) if you were giving instructions to a person -- a
fairly simple-minded person who is excellent at following instructions
exactly as given (no matter how wrong they may be), very quickly.
Read that over, trying to "think" like such a person. Maybe even
simulate it step by step by writing down the values of variables.
When that gives you the results you want, then start translating it
from English to Ruby. That's how most people start out learning to
program. When you get good at it, then you can skip the English step.

-Dave

--
Posted via http://www.ruby-forum.com/\.

Oh! They're usually covered more or less together.

Also, as you may have figured out, anything you can do with either
one, you can also do with the other. (Though it will often take
sprinkling in some negations too.) When to use which one, is mainly a
matter of making the code clear, though sometimes there may be
efficiency benefits too.

-Dave

···

On Sat, Dec 10, 2011 at 08:53, Emeka Patrick <emekapatrick@gmail.com> wrote:

Didn't cover && as of yet in the book
so wasn't aware of using it as an option.

--
LOOKING FOR WORK! What: Ruby (on/off Rails), Python, other modern languages.
Where: Northern Virginia, Washington DC (near Orange Line), and remote work.
See: davearonson.com (main) * codosaur.us (code) * dare2xl.com (excellence).
Specialization is for insects. (Heinlein) - Have Pun, Will Babble! (Aronson)