Jesus Diaz Jesus Diaz - 6 months ago 12
Ruby Question

Basic Ruby Exercise - Method and If

I am doing a Ruby exercise, but I can't figure out what is wrong. I need to modify the

suma
method so my tests both return true.

I'm not allowed to add another if, or else, remove code or modify arguments. Am I missing something?

def suma(a, b)
"#{a} es mayor que #{b}" if a > b
"#{a} no es mayor que #{b}"
end

#test
p suma(10, 5) == "10 es mayor que 5"
p suma(4, 23) == "4 no es mayor que 23"

Answer

You're missing a return in the first conditional.

def suma(a, b)
  return "#{a} es mayor que #{b}" if a > b
  "#{a} no es mayor que #{b}"
end

Ruby by default will return the last statement in a method, but it allows for early returns. Since you want to return early if a > b, you have to tell Ruby as much. Otherwise, it will execute the line and default to the last statement in the method for its return.