Zack Shapiro
Zack Shapiro

Reputation: 6998

Basic If/Else statement question

I'm getting a strange error: "syntax error, unexpected $end, expecting kEND" and it points to the final line of my code.

New to Ruby and not sure what I'm doing wrong here. Any help would be fantastic. Thanks!

 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)

Upvotes: 1

Views: 251

Answers (4)

David Kumar
David Kumar

Reputation: 1

wap to input principle, rate, time and choice. if choice is 1 than calculate simple interest, if choice is 2 calculate compund interest and if it is other than 1 or 2 print "Enter valid choice"

Upvotes: 0

Pavling
Pavling

Reputation: 3963

BTW, you can refactor your if..end statement out completely if you prefer

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

Upvotes: 6

Caspar
Caspar

Reputation: 7759

Both if statements and function definitions require an end statement to terminate them.

Try adding another end after your existing end and your problem should go away.

Upvotes: 2

sparkymat
sparkymat

Reputation: 10028

Corrected code (you are missing one end for the 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)

Upvotes: 5

Related Questions