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

filibonio hernandez
filibonio hernandez
2,826 Points

Can some one please explain how this code worked.

Hey guys so I am on the && operator code challenge and took me a bit of time to figure this one out and I finally did but don't fully understand how? this is my code that got in with a pass:

def check_speed(car_speed)

if car_speed >= 40 && car_speed <= 50 return "safe" else return "unsafe" end end

I understand why the if statement worked but I don't understand on the line where it says return why did I "have " to put "return" and not the "puts" command.

and I put the code in the workplace to see what it does but nothing happens but I'm guessing the way the code is written it is not supposed to because I am not telling ruby to put anything besides the "If" statements. Right?

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! Essentially, the reason you have to return the value is because the challenge demands it because of some mechanism they use to grade it. Ruby by default returns a value and there is some debate over whether you should ever explicitly return a value in Ruby.

Here's the part of the instructions that make this a requirement:

The method below checks the speed of a car and returns a string value: either "safe" or "unsafe". Return "safe" if:

Here, the challenge explicitly states that you must return a value. Your puts statement simply prints/displays the value.

Other than to say that it is a requirement of the challenge, there would likely be no point in explicitly returning the value.

Hope this helps! :sparkles: