简体   繁体   English

如何防止 Lint/语法:方法主体中的 class 定义和 Lint/语法:ruby linters 中的意外令牌 tEQL

[英]How to prevent Lint/Syntax: class definition in method body and Lint/Syntax: unexpected token tEQL in ruby linters

I'm currently learning ruby so I created this simple class我目前正在学习 ruby 所以我创建了这个简单的 class

class Person
  attr_accessor :id, :name, :age
  
  def initialize(name='Unknow', age, parent_permission = true)
    @id = 222
    @name = name
    @age = age
  end
end

but when y ran linters they showed me this two errors但是当你运行 linter 时,他们向我展示了这两个错误

在此处输入图像描述

I reorganized the parameters passed to my initialize method and linters stopped showing those errors, but I'm not really sure which was the mistake or how i should organize parameters in order to prevent this kind of errors.我重新组织了传递给我的初始化方法的参数,并且 linter 停止显示这些错误,但我不确定哪个是错误或我应该如何组织参数以防止此类错误。 Can someone please help me?有人可以帮帮我吗? just in case it is required I'm using Ruby 3.1.2 and rubocop 1.34.1以防万一我使用 Ruby 3.1.2 和 rubocop 1.34.1

thanks in advance提前致谢

I can reproduce the class definition in method body warning, and I think you've found a bug in RuboCop.我可以class definition in method body ,并且我认为您在 RuboCop 中发现了一个错误。 The complication relates to optional parameters – these need to come last, otherwise there may be ambiguity in the caller.复杂性与可选参数有关——这些参数需要放在最后,否则调用者可能会产生歧义。

If you change it to age, name='Unknown', parent_permission = true there is no warning.如果将其更改为age, name='Unknown', parent_permission = true则没有警告。

RuboCop should complain about this, but the warning of class definition in method body is wrong and misleading. RuboCop应该抱怨这一点,但class definition in method body的警告是错误的且具有误导性。 I would suggest reporting this as bug in the RuboCop GitHub repo.我建议将此报告为 RuboCop GitHub 存储库中的错误。

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

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