Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,274 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