繁体   English   中英

Rails项目的语法错误

[英]Syntax error for Rails project

我是Rails和Ruby的初学者,我正在开发一个用于跟踪玩家及其胜利,损失和elo的桌子的elo系统。

现在我收到了错误

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

我已经搜索了所有文件,找不到关闭的paren或支架,但找不到任何东西。 以下是我的代码的一些片段:

PlayerController索引

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

PlayerModel方法

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

如果有人能帮助我,我将不胜感激。

编辑:根据要求,这是我的Player模型类的第27-36行

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

这是问题:

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

它应该是 :

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

你的(6 < var <= 15)是有效的语法,但没有意义。 这意味着(6 < var) <= 15 由于6 < var将为truefalse ,因此它将被计算为true <= 15false <= 15 ,除非您奇怪地重写了<=>否则将导致错误。

你可能打算(6 < var and var <= 15)


更新

你有两个问题。

  • 你有else ...... endif ...... end 你可能想if ... else ... end
  • 你有一个额外的end结尾。

你的代码看起来不太好看。 应该是这样的:

 def win_percentage return 0.001 if (wins.count + lose.count).zero? (wins.count * 6) - (lose.count * 4) end 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM