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 Basics Conditionals "if" Statements

Paul King
PLUS
Paul King
Courses Plus 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,046 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.