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

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...