Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing keywords error on initializing a constructor

I have a Source model and ArticlesController. When user clicks scrape button, the control is passed to below mentioned ArticlesController#Scrape. The scrape then calls Source model where the sources are being initialised and the list of articles are returned in a form of hash to articles inside Scrape. Source Model -

class Source
    attr_accessor :source_name, :source_url, :source_type, :source_key, :tag_name

    def self.all_instances
      @@array
    end

    # Default constructor
    def initialize

    end

    def initialize(source_name:, source_url:, source_type:, source_key:, tag_name:)
        @source_name = source_name
        @source_url = source_url
        @source_type = source_type
        @source_key = source_key
        @tag_name = tag_name
        @@array << self
    end
    def init

        self.new('The Age',
                 'http://www.theage.com.au/rssheadlines/victoria/article/rss.xml',
                 'RSS',
                 '',
                 'Victoria News')
    end

    def import
        init()
        //returns hash of articles back
    end
end 

class ArticlesController < ApplicationController
    def scrape
        @get_Articles = Source.new
        articles = @get_Articles.import
        //stores articles in article model
        //redirect to article path
    end
end

I am getting ArgumentError in ArticlesController#scrape on @get_Articles = Source.new

Inside Source class the constructor def initialize(source_name:, source_url:, source_type:, source_key:, tag_name:) is being called. To rectify the issue I created a default constructor also, so that the parameterized constructor doesn't get called. However, I am not sure how to fix this problem. Could somebody please help?

like image 378
coder05 Avatar asked Sep 12 '26 07:09

coder05


1 Answers

I think you are doing it wrong with the def initialize method. You don't want parameterized constructor just removed it. if you want this as well then you need to handle this for null values also. Just creating a default constructor will not solve the issue because it will be override with other one.

You can try like this

def initialize(options ={})
  @source_name = options[:source_name] if options[:source_name].present?
  #handle and assign other keys and values similer to this
  @@array << self
end

now you can use this as

@get_Articles = Source.new

or

@get_Articles = Source.new(source_name: "abc")
like image 160
Prakash Laxkar Avatar answered Sep 13 '26 22:09

Prakash Laxkar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!