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

Michael Henry
PLUS
Michael Henry
Courses Plus Student 811 Points

Why is using "Return "safe"" right when "Puts "safe"" wrong?

So far in the lessons, we have been instructed to use "puts", and I learned in the community that "return" is the right. I found this also in the community: https://teamtreehouse.com/community/what-is-the-difference-between-puts-vs-return-when-defining-a-method

My big point (&&) question, did I miss something in the instruction up-to-this point because we never talked about "return".


Why is this the right answer:

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

when this is this is the wrong answer"

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

1 Answer

Ari Misha
Ari Misha
19,323 Points

Hiya there! "return" is the right way coz the challenge asked you to return the variable , not output to the console. Also one more thing i'd like to mention here is that you dont even need to use return keywords in your loops or function. Why? 'coz Ruby does an implicit return and assumes the last statement is returning something. So finally your code should look like this:

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