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

Could it be done like this to make the code abit better ?

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

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

2 Answers

Tim Knight
Tim Knight
28,888 Points

Kamal,

Yes it could just keep in mind however that you're currently returning true as a string and you don't need to specifically tell ruby to return. You'll also want to compare command to the yes and y as strings so don't forget those quotes.

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

You might also want to just downcase the command value once and you can remove those parentheses:

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

Or like this:

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

Finally another option might be a ternary operator where you could return false if needed:

def valid_command?(command)
  command = command.downcase
  (command == "yes" || command == "y") ? true : false
end

thanks for that