Home > Net >  Wrong number of arguments Ruby while instantiating a Subclass
Wrong number of arguments Ruby while instantiating a Subclass

Time:10-29

I have 3 classes that hare related through inheritance (it's a course exercise)

I have a Brand < Product < DBHandler classes

They are in a subfolder of the project, and all of them use require to access each other:

├── class
│   ├── Brand.rb
│   ├── Category.rb
│   ├── DBHandler.rb
│   └── Product.rb
├── db
│   ├── catalogo_categorias.txt
│   ├── catalogo_marca.txt
│   ├── catalogo_producto.txt
│   └── inventario_final.txt
├── init.rb
└── README.md

Clases

The Product file has in it:

require "#{Dir.pwd}/class/DBHandler"

class Product < DBHandler
  attr_reader :key, :final_price, :db
  attr_accessor :name, :unit, :brand, :category, :price

  def initialize(name, unit, brand, category, price)
    @name = name
    @unit = unit
    @brand = brand
    @category = category
    @price = price
    @final_price = set_final_price
    @db = DBHandler.new
    @key = set_key
  end

  def set_key
    @key = "#{@db.how_many 1}#{self.name[0..2].upcase}3BS"
  end

  def set_final_price
    @final_price = self.price * 1.19
  end

  def search_product(search_expresion)
    @db.db_search(search_expresion)
  end

  def alta_product
    @db.write(
      "#{key},#{name},#{unit},#{brand},#{category},#{price},#{final_price}\n"
    )
  end
end

and the Brand file has:

require "#{Dir.pwd}/class/Product"

class Brand < Product
  attr_reader :brand_key, :a_brands, :q_brands
  attr_accessor :brand

  def initialize(brand)
    # use super to use parent's attributes
    super(brand)
    @brand_key = set_brand_key
    @a_brands = get_file.read
                        .split("\n")
                        .map { |abrand| abrand.split(',') }
    @q_brands = @a_brands.count
  end

  def set_brand_key
    "#{@q_brands}#{self.brand[0..2].upcase}3BS"
  end

  def get_file
    File.open("#{Dir.pwd}/db/catalogo_marca.txt")
  end

  def alta_brand
    get_file.write("#{@brand_key},#{@brand}", mode: 'a')
  end
end

Init file

All the files are called from the init.rb file in order to call some instance methods.

this is my Init.rb file first lines.

require './class/Product'
require './class/Brand'
require './class/DBHandler'
require './class/Category'

The problem

when I want to create a new instance of Brand class, I have tried:

  1. brand = Brand.new(name, unit, brand, category, price) and it returns wrong number of arguments (given 5, expected 1) (ArgumentError)
  2. brand = Brand.new(brand) and it returns wrong number of arguments (given 1, expected 5) (ArgumentError)

I don't know what may I doing wrong. I need to create a Brand instance with only brand argument

CodePudding user response:

Your problem is here

super(brand)

This is saying "call the parent constructor with brand as the only argument". The parent constructor is Product#initialize, which takes five arguments. So no matter how you call Brand#initialize, it's going to fail since it calls Product#initialize with the wrong number of arguments. You need to call super with all five.

But I challenge the frame. Subclassing is a very tight coupling, an "is-a" relationship. What you're claiming is that every brand is a product, and I fail to see how that's true. So rather than fixing the super call, you might reconsider your design and ask yourself if you really meant to make a subclass.

  • Related