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

return was not covered in the previous video tutorial. how do i set the return value to equal a string?

how to set the return value

ruby.rb
def check_speed(car_speed)
  # write your code here
  if (car_speed > 40) && (car_speed <= 50)
    return 'safe'
end

1 Answer

Cauli Tomaz
Cauli Tomaz
12,362 Points

Ruby doesn't require the "return" statement. It will always return the last variable of the function.

You can use it to force-exit from a function early though (http://stackoverflow.com/questions/4601498/what-is-the-point-of-return-in-ruby)

def check_speed(car_speed)
  warning = 'unsafe'
  if (car_speed > 40) && (car_speed <= 50)
    warning = 'safe'
  end
  warning
end

or

def check_speed(car_speed)
  if (car_speed > 40) && (car_speed <= 50)
    'safe'
  else
    'unsafe'
  end
end
Cauli Tomaz
Cauli Tomaz
12,362 Points

Edit: I forgot to add an 'end' to the if statement. This is also your case, and this might be causing your problem.

its still not working... hmmm

Cauli Tomaz
Cauli Tomaz
12,362 Points

The problem is logic, not the ruby syntax.

I've read the problem and it states "The speed must be at least 40" and you are using (car_speed > 40)

This makes a speed of at least "41"!

nope. still not working

Cauli Tomaz
Cauli Tomaz
12,362 Points

Correct answer, tested:

def check_speed(car_speed)
  if (car_speed >= 40) && (car_speed <= 50)
    'safe'
  end
end

Thank You, Thank you...