1

I'm a beginner to Rails and Ruby, and I'm working on an elo system for a table that tracks players and their wins, losses, and elo.

Right now I'm getting the error

app/models/player.rb:36: syntax error, unexpected keyword_end, 
expecting end-of-input):
app/controllers/players_controller.rb:3:in `index'

I've scoured all my files for a missing closing paren or bracket, but can't find anything. Here are some snippets of my code:

PlayerController index

def index
@players_items = Player.all.sort{|y,x| x.get_elo() <=> y.get_elo()}
end

PlayerModel methods

def get_elo()
    return self.elo
end

def update_weight()
    var = wins.count + lose.count
    if(var <= 6)
        self.weight = 50
    elsif(6 < var and var <= 15)
        self.weight = 25
    else
        self.weight = 15
    end
end

def update_elo(p2_elo, result)
    p1_elo = self.elo
    expected_score = 1 / (1 + 10 ** ((p2_elo - p1_elo)/400))
    self.elo += (self.weight * (result - expected_score)).round
end

If anyone could help me out, it would be greatly appreciated.

EDIT: As requested, here is lines 27-36 of my Player model class

def win_percentage()
    var = wins.count + lose.count
        if(var == 0)
            return 0.001
    end
    else
        return ((wins.count * 6) - (lose.count * 4))
    end
end
end
9
  • 1
    This is invalid: elsif(6 < var <= 15)
    – lurker
    Jul 16, 2013 at 19:28
  • 1
    The error (which has a copy/paste issue because it has itself some syntax error, extra paren) is at player.rb line 36, but you only show part of that file so we can't tell which line is 36. The model is called from the controller, and the error is showing that as well. Can you show the lines around line 36 in player.rb?
    – lurker
    Jul 16, 2013 at 19:31
  • 1
    I've edited my main post with lines 27-36 of my player model class at the bottom Jul 16, 2013 at 19:34
  • 1
    Don't write that information here. Write the line numbers in the question.
    – sawa
    Jul 16, 2013 at 19:35
  • 2
    In your added code you have an else after your end. @Priti points out the error.
    – lurker
    Jul 16, 2013 at 19:37

2 Answers 2

2

here is the issue:

if(var == 0)
            return 0.001
    end # <~~ why this end keyword ?
    else
        return ((wins.count * 6) - (lose.count * 4))
    end

It should be :

if(var == 0)
  return 0.001
else
  return ((wins.count * 6) - (lose.count * 4))
end
0
1

Your (6 < var <= 15) is valid syntax, but does not make sense. It means (6 < var) <= 15. Since 6 < var would be true or false, it would then be evaluated as true <= 15 or false <= 15, which will cause an error unless you have strangely overridden <=>.

You probably intended (6 < var and var <= 15).


Update

You have two problems.

  • You have else...end after if...end. You may have wanted if...else...end.
  • You have an extra end at the end.

Your code does not look nice. It should be like:

def win_percentage
  return 0.001 if (wins.count + lose.count).zero?
  (wins.count * 6) - (lose.count * 4)
end
1
  • Oh I was unaware the logic syntax was written that way in Ruby. Unfortunately I tried that and it didn't resolve my issue. From the error message, the line highlighted with the error is @players_items = Player.all.sort{|y,x| x.get_elo() <=> y.get_elo()} Jul 16, 2013 at 19:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.