Ruby Ruby Basics Conditionals "if" Statements

Paul King
PRO
Paul King
Pro Student 776 Points

Hi i am stuck on a question.

def check_speed(55) if check_speed > 55 puts "too fast" end if check_speed < 55 puts "too slow" end if check_speed == 55 puts "speed OK" end end

program.rb
def check_speed(55)
  if check_speed > 55
    puts "too fast"
  end
  if check_speed < 55
    puts "too slow"
  end

  if check_speed == 55
    puts "speed OK"
  end
end

1 Answer

Mustafa Başaran
Mustafa Başaran
28,018 Points

Hi Paul,

if block should finalize with one end. Checking a condition with elsif and leaving all other possible cases to be handled with else statement are all parts of the same if block. One other very important thing.. you should introduce a parameter in your function, which you can replace with variables when you call the function for later use.

So, it should read something like this:

def check_speed(speed)
  # check if speed is above 55 & print output accordingly.
  # check else if speed is below 55 & print output accordingly.
  # check all other options & print output accordingly.... 
  # well, there is only one remaining alternative outcome.  
  # speed is an integer, right? :)
end

I hope this helps.