Ruby Ruby Basics Conditionals "if" Statements

Georgios Mavropoulos
PRO
Georgios Mavropoulos
Pro Student 3,176 Points

Can we use range in an "if" statement?

Can we use range to write this code?

program.rb
def check_speed(speed)
  if speed < 45
    puts "too slow"
  end
  if speed > 60
    puts "too fast"
  end
    if speed == (45..60)
    puts "speed OK"
  end
end

1 Answer

Steve Hunter
MOD
Steve Hunter
Treehouse Moderator 56,301 Points

Yep .... here

However, your example doesn't need it as the options are mutually exclusive - there's no need for the last test as the number can only be within the range following the first two tests, so that just needs an else clause to output "speed OK".

Steve.