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

Ruby operators code challenge

<h1>This is the challeng: </h1> Modify the "valid_command?" method to return true when passed the following values: "y", "yes", "Y", or "YES".

Here is what I tried:

def valid_command?(command) if (command == "y") || (command == "yes") || (command == "Y") || (command == "YES") valid_command? = true end end

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

Why is this not working?

Removing the parenthesis (since you already used double quotes) will solve the problem.

def valid_command? (command)

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

return "true"

end

end

5 Answers

You got most of it right. I don't have the depth of knowledge to guide you towards an answer, but I know what it should be:

def valid_command?(command)
  if (command == "y") || (command == "yes") || (command == "Y") || (command == "YES")
    return true
  end 
end

Personally, I don't know why you can't just say 'return' instead of 'return true', because I would have thought that 'return' would return 'true' i.e. the method would evaluate to true.

Maybe someone can enlighten us.

EDIT: the code is 'DRY-ist' as:

def valid_command?(command)
  command == "y" || "yes" || "Y" || "YES" 
end

Personally, this is how I would write it - it's readable enough for most developers.

Thanks for the quick response

are these '||' the same as &&?

Wilfredo Casas , no; it means 'or'.

codebully
codebully
2,306 Points

You can DRY this up with as well:

def valid_command?(command)
  if (command.downcase == "y") || (command.downcase == "yes")
    return true
  end 
end

are these '||' the same as &&?

adrian silva
adrian silva
4,959 Points

i used the case and when condition but that didnt work i just want to know why or is it just how it is ? def valid_command?(command) case command when command == "y" return true when command == "yes" return true when command == "Y" return true when command == "YES" return true end end

Sandra Hogan
Sandra Hogan
8,657 Points

Wilfredo Casas '||' and '&&' are 2 different symbols. The pipes( || ) mean 'or' and the '&&' mean 'and'