0

I have a small code for determining the number of hours per year.

What I am looking for is for a Ruby way to allow different user's input options for all possible valid answer stored in an array to check if the user provided a valid option by calling include? on the array.

When year consist of 52 weeks. Week consists of 7 days. Day consists of 24 hours

My problem comes up when trying to pass different answers for the gets method such as follows:

if answer == "yes" || "yup" || "yeah" || "positive";
if answer == ("yes", "yup", "yeah", "positive")
if answer == ["yes" or "yup" or "yeah" or "positive"]

I receive an error which I couldn't solve

answer = gets.chomp
if answer == "yes" or "yup" or "yeah" or "positive"
  puts "Good!"
  puts desc_text
  my_var = gets.chomp.to_i
  if my_var == 3736
    puts good_text
  else
    puts wrong_text
    puts bad_text
  end
elsif answer == [ "no" || "nop" || "nay || ""negative" ]
  puts bad_text
else
  puts yes_no
end

I'd like to pass different answer options such as yes, yup, yeah, positive instead of just enclose me to a yes and/or no answer

  • 2
    `if ["yes", "yup", "yeah", "positive"].include?(answer)` or `if asnswer == "yes" or answer == "yup" or answer == "yeah" or answer == "positive"`. – Aleksei Matiushkin Aug 25 '19 at 04:13
  • 4
    "I receive an error which I couldn't solve" is not a precise enough error description for us to help you. *What* doesn't work? *How* doesn't it work? What trouble do you have with your code? Do you get an error message? What is the error message? Is the result you are getting not the result you are expecting? What result do you expect and why, what is the result you are getting and how do the two differ? Is the behavior you are observing not the desired behavior? What is the desired behavior and why, what is the observed behavior, and in what way do they differ? – Jörg W Mittag Aug 25 '19 at 05:03
  • 1
    When you're first learning Ruby it's natural to think `answer == "yes" || "yup" || "yeah"` works as it reads: "answer is 'yes' or 'yup' or 'yeah'", but it doesn't work that way. You need, `answer == "yes" || answer == "yup" || answer == "yeah"`, though that would normally be written `["yes", "yup", "yeah"].include?(answer)`. – Cary Swoveland Aug 25 '19 at 05:34
  • Let's look at the wrong way I initially mentioned in my comment above. The right side of `==` is evaluated first, left to right: `"yes" || "yup" #=> "yes"`, then `"yes" || "yeah" #=> "yes"` (`#=> x` meaning `x` is the result of the calculation), so the original statement is equivalent to `answer == "yes"`. I expect someone will explain in an answer why that is so. It has to do with logically `true` ("truthy") expressions and logically `false` ("falsy") expressions. – Cary Swoveland Aug 25 '19 at 05:34
  • @AlekseiMatiushkin why not post an answer, you gave it in your first comment – max pleaner Aug 25 '19 at 05:48
  • @maxpleaner: If that is indeed the question the OP has, which is not at all clear from the question, because the question doesn't actually contain a question or a problem description, then the question is a duplicate of dozens of other questions and should be closed as a duplicate instead of fragmenting answers on the site. – Jörg W Mittag Aug 25 '19 at 10:27
  • Possible duplicate of [Check if a value exists in an array in Ruby](https://stackoverflow.com/questions/1986386/check-if-a-value-exists-in-an-array-in-ruby) – Mr. Aug 25 '19 at 14:43
  • Thanks to all!! I read every single comment and each one it's of great help. The most direct solution to my problem was if ["yes", "yup", "yeah", "positive"].include?(answer) As AlekseiMatiushkin and @CarySwoveland suggested – Lazy Cougar Aug 26 '19 at 01:44

2 Answers2

0

A more idiomatic Ruby way of allowing for these different user options would be to have all possible valid user input stored in an array, then checking if the user provided a valid option by calling include? on the array. This way, you can update the array with more possible options at a later time. Generally arrays like this are stored as constants, though you can also store them as local variables. I find that for a script like this, variables are fine, but if I'm writing a larger program and I need arrays like this (for example, arrays that specify valid options) in a class, then I use constants to make it easy for other contributors to find them. Another option yet would be to have a configuration file (can be a Ruby file, yaml file, JSON file...) that defines valid options, and then you can load this file when executing your program.

Here is an example of what I mean:

VALID_AFFIRMATIVE_ANSWERS = %w(yes yup yea positive)
VALID_NEGATIVE_ANSWERS = %w(no not nay negative)

answer = gets.chomp
if VALID_AFFIRMATIVE_ANSWERS.include?(answer)
  puts "Good!"
  puts desc_text
  my_var = gets.chomp.to_i
  if my_var == 3736
    puts good_text
  else
    puts wrong_text
    puts bad_text
  end
elsif VALID_NEGATIVE_ANSWERS.include?(answer)
  puts bad_text
else
  puts yes_no
end

I'm not sure what errors you're receiving, but I'm not sure if you're ever defining some of these variables (for example desc_text etc.).

aceofbassgreg
  • 3,837
  • 2
  • 32
  • 42
0

if code is excetly as you write above your issue is in line written below

elsif answer == [ "no" || "nop" || "nay || ""negative" ]

although it won't work , so the solution for you would be like below

def process_positive_response
  my_var = gets.chomp.to_i
  if my_var == 3736
   puts good_text
  else
   puts wrong_text
   puts bad_text
  end
end 


answer = gets.chomp
case answer
when 'yes','yup','yeah','positive'
  puts "Good!"
  puts desc_text
  process_positive_response
when 'no', 'nop', 'nay', 'negative'
  puts bad_text
else
  puts yes_no
end 

first why write so much conditions when ruby already does that for you with case statement, and second distribute your code in chunks so it will make your code more traceable.

Ashok Damaniya
  • 303
  • 1
  • 6
  • Perfect! You are totally right in regards to the case statement instead of branching with IFs and ELSIFs. I will try now everything with case statement. Thanks! – Lazy Cougar Aug 26 '19 at 19:16