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

gerald blady
gerald blady
9,052 Points

If Statement and &&

ruby.rb
def check_speed(car_speed)
  if (car_speed > 39) && (car_speed <= 50)
    return "safe"
end

2 Answers

Looks like you forgot to close the if statement.

gerald blady
gerald blady
9,052 Points

you caught me while updating.

Where are you referring to? as I see all ( ) are closed?

All you need is an end after the line with return "safe".

def check_speed(car_speed)
  if (car_speed > 39) && (car_speed <= 50)
    return "safe"
  end
end
gerald blady
gerald blady
9,052 Points

Ah! thank you for the set of eyes as I was looking at the end, not thinking I needed two. Simple mistake. appreciate it.

No worries! It's easy to overlook, especially since many languages allow you to not close if statements when only a single line is present. The "ruby way" to achieve this would be to put it all on one line:

def check_speed(car_speed)
  return "safe" if (car_speed > 39) && (car_speed <= 50)
end

But sometimes that can be hard to read, so it's a judgement call.