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 trialAnton Arboleda
2,018 PointsPuts, 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
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
Treehouse Moderator 145,860 PointsHey Anton,
You've pretty much got it, except for 2 things:
The use of
elsif
is limited to 3 or more options in the checks. Here there are 2 options, so you will need to useelse
.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! :)
John Baulig
9,180 PointsAnton,
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
Anton Arboleda
2,018 PointsAnton Arboleda
2,018 PointsMakes sense, thanks for the help Jason!!!!