基本的If/Else语句问题

Zac*_*iro 1 ruby syntax if-statement

我收到一个奇怪的错误:"语法错误,意外的$ end,期待kEND",它指向我代码的最后一行.

Ruby的新手,不知道我在这里做错了什么.任何帮助都会很棒.谢谢!

 def add(x,y)
      if(x > y)
        c = x + y
        return c

  else
    puts "Y is too big"
    return   
end


a = 4
b = 6

add(a,b)
Run Code Online (Sandbox Code Playgroud)

Pav*_*ing 6

顺便说一句,如果您愿意,可以完全重构if..end语句

def add(x,y)
  return (x + y) if(x > y)
  puts "Y is too big"
end
Run Code Online (Sandbox Code Playgroud)


spa*_*mat 5

更正的代码(你缺少if-else的一端):

def add(x,y)
    if(x > y)
        c = x + y
        return c
    else
        puts "Y is too big"
        return   
    end
end

a = 4
b = 6

add(a,b)
Run Code Online (Sandbox Code Playgroud)