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 And (&&) Operator

Anton Arboleda
Anton Arboleda
2,018 Points

Puts, return and defining a method

I am having a very hard time grasping this challenge. I know it says to 'return' "safe" and "unsafe" but do not understand how to return it depending on the car_speed. I am very new to Ruby. I understand the && concept mentioned in the video however, I don't understand how to "return safe if" ...etc

ruby.rb
def check_speed(car_speed)
  # write your code here
  if (car_speed >= 40) && (car_speed <= 50)
    safe
  elsif
    unsafe
  end

end

2 Answers

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,858 Points

Hey Anton,

You've pretty much got it, except for 2 things:

  1. The use of elsif is limited to 3 or more options in the checks. Here there are 2 options, so you will need to use else.

  2. You need quotation marks around the "safe" and "unsafe" strings. As it is now, Ruby thinks you are trying to return variables.

def check_speed(car_speed)
  # write your code here
  if (car_speed >= 40) && (car_speed <= 50)
    return "safe"
  else
    return "unsafe"
  end
end

Also, just a note and my personal preference. I know that Ruby explicitly returns things, so the need for the return keyword is not necessary; however, for readability and clarity, I do recommend using it. It makes quickly scanning the code much easier and way more clear. (Again, just my opinion and experience).

Keep Coding! :)

:dizzy:

Anton Arboleda
Anton Arboleda
2,018 Points

Makes sense, thanks for the help Jason!!!!

Anton,

I hope the code below helps:

I believe the main issue is from safe and unsafe not being formatted as strings in your code - that trips me up from time to time as well.

def check_speed(car_speed)
  if  car_speed >= 40 && car_speed <= 50
    "safe"
  else
    "unsafe"
  end
end