Home > Net >  How to prevent Lint/Syntax: class definition in method body and Lint/Syntax: unexpected token tEQL i
How to prevent Lint/Syntax: class definition in method body and Lint/Syntax: unexpected token tEQL i

Time:08-22

I'm currently learning ruby so I created this simple 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

enter image description here

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. Can someone please help me? just in case it is required I'm using Ruby 3.1.2 and rubocop 1.34.1

thanks in advance

CodePudding user response:

I can reproduce the class definition in method body warning, and I think you've found a bug in 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.

RuboCop should complain about this, but the warning of class definition in method body is wrong and misleading. I would suggest reporting this as bug in the RuboCop GitHub repo.

  • Related