Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error while passing argument list to Superclass

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading