Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Ruby Operators and Control Structures Logical Operators The Or (||) Operator

I think the questions need to be little bit more clear. When you say it has to return true.

I think the questions need to be little bit more clear. When you say it has to return true. Should one construct the whole if, else including the users input or you just want a if, else in the definition.

ruby.rb
def valid_command?(command)
  if(command == "y" && command == "Y") || (command == "yes" && command == "YES")
    return true
  else
    return false
  end  
end

2 Answers

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

Construction like this:

command == "y" && command == "Y"

means that you want command to be equal to 'y' and 'Y' at the same time, which will never happen and you will always return false (and there is nothing about returning false in the question). The simplest code that passes:

  return true if command == "y" || "yes" || "Y" || "YES"

Again, there is nothing about an else in the question.

William Li
William Li
Courses Plus Student 26,868 Points

Hi, Maciej, actually the simplest code that passes is

def valid_command?(command)
  command == "y" || "yes" || "Y" || "YES"
end
Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

True that :). I just wanted to make is sound exactly like the question.

William Li
William Li
Courses Plus Student 26,868 Points

:) but this expression command == "y" || "yes" || "Y" || "YES" is really neat, did Jason teach that during the lecture? I don't think he did, right? wish there's a way to promote this answer at the forum

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

I think it would be too overwhelming for people who are new to programming. It was easy for me to understand method/function returns after programming in C and Java. It was just a matter of switching to the mode where the return is implicit and I don't have to write it anymore. But if Ruby is someone's first programming language, and these videos are their first tutorial, it could be very confusing (at least when I think about it and try to put myself in newbie shoes).

thanks.

yes, also I think he didnt mention about ? next to the method name. Unless I missed it.