Home > database >  Error while passing argument list to Superclass
Error while passing argument list to Superclass

Time:10-24

Sorry if the question is unclear as I'm new to Ruby. I have a Parent class (not implemented by me) Reports that has a method query defined as:

class Reports

      def self.query(id:, is_active: false, is_timestamp: false, version: DEFAULT_VERSION, limit: DEFAULT_LIMIT)
    
         <<code>>
end

I have defined a child class Report that inherits Reports and defined the same method query as below:

class Report < Reports

    def self.q_version(id)
           <<some logic to decide version>>
    end
    def self.query(id:, is_active: false, is_timestamp: false, limit: DEFAULT_LIMIT)
        version = q_version(id)
        super(id, is_active, is_timestamp, version, limit)
    end

Now when I run the code, I'm getting an argument error as such:

ArgumentError: wrong number of arguments (given 5, expected 0; required keyword: id)

I suspect I'm not doing the super call correctly but couldn't figure out which part.

so any help would be appreciated.

CodePudding user response:

The method's signature tells that it expects keyword arguments like this:

def self.query(id:, is_active: false, is_timestamp: false, limit: DEFAULT_LIMIT)
  version = q_version(id)
    
  super(id: id, is_active: is_active, is_timestamp: is_timestamp, version: version, limit: limit)
end

Or you could use the new shorthand hash syntax when you are on Ruby 3.1 :

def self.query(id:, is_active: false, is_timestamp: false, limit: DEFAULT_LIMIT)
  version = q_version(id)
    
  super(id:, is_active:, is_timestamp:, version:, limit:)
end
  •  Tags:  
  • ruby
  • Related