Michael Liu
Michael Liu

Reputation: 1131

Syntax error for Rails project

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

Upvotes: 1

Views: 112

Answers (2)

sawa
sawa

Reputation: 168081

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

Upvotes: 1

Arup Rakshit
Arup Rakshit

Reputation: 118261

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

Upvotes: 2

Related Questions