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

Andrew Carr
Andrew Carr
10,979 Points

Why is my return value to "safe" for setting 'check_speed(car_speed)' not working?

Question is such: Set the return value of the "check_speed" method to the string "safe" depending on the following condition car_speed is greater than forty and less than fifty.

Code is such:

def check_speed(car_speed)

if car_speed >= 40 && car_speed <=50

puts "safe"

end

What am I not reading correctly? Thanks!

4 Answers

def check_speed(car_speed)

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

end

instead of puts it needs to be return and that will fix the problem

ARNOLD SANDERS
ARNOLD SANDERS
5,266 Points

you are missing a second end.

Andrew Carr
Andrew Carr
10,979 Points

That may have been part of it, but I wasn't setting the car_speed argument to "safe", which I think is sort of an odd thing to do instead of just outputting safe or not safe and leaving the argument as an integer.

ARNOLD SANDERS
ARNOLD SANDERS
5,266 Points

The car speed argument is an integer . Its not a string called 'safe'. What you want to return is the string 'safe'. If you ran the method with a string instead of whats expected, it will give you a comparison error. Your code was correct as you wrote it, it was simply missing a second end. check out www.rep.it and see for yourself. Heres a small program to check the code:

 def check_speed(car_speed)

if car_speed >= 40 && car_speed <=50

puts "safe"

end

end

print 'Enter a check_speed argument' 
speed = gets.chomp
    case speed
      when '40'
      '40'.to_i
      check_speed(40)
      when 'safe'
      check_speed('safe')
      else
         exit
    end