Ruby - initialize inheritance, super with only certain arguments?
<p>I've been playing around with Ruby as of late and I can't seem to find the answer to my question.</p>
<p>I have a class and a subclass. Class has some <code>initialize</code> method, and subclass has its own <code>initialize</code> method that is supposed to inherit some (but not all) variables from it and additionally add its own variables to the subclass objects.</p>
<p>My <code>Person</code> has <code>@name</code>, <code>@age</code> and <code>@occupation</code>.</p>
<p>My <code>Viking</code> is supposed to have a <code>@name</code> and <code>@age</code> which it inherits from <code>Person</code>, and additionally a <code>@weapon</code> which <code>Person</code> doesn't have. A <code>Viking</code> obviously doesn't need any <code>@occupation</code>, and shouldn't have one.</p>
<pre><code># doesn't work
class Person
  def initialize(name, age, occupation)
    @name = name
    @age = age
    @occupation = occupation
  end
end

class Viking &lt; Person
  def initialize(name, age, weapon)
    super(name, age) # this seems to cause error
    @weapon = weapon
  end
end

eric = Viking.new(&quot;Eric&quot;, 24, 'broadsword') 
# ArgError: wrong number of arguments (2 for 3)
</code></pre>
<p>You can make it work in the following ways, but neither solution appeals to me</p>
<pre><code>class Person
  def initialize(name, age, occupation = 'bug hunter')
    @name = name
    @age = age
    @occupation = occupation
  end
end

class Viking &lt; Person
  def initialize(name, age, weapon)
    super(name, age)
    @weapon = weapon
  end
end

eric = Viking.new(&quot;Eric&quot;, 24, 'broadsword') 
# Eric now has an additional @occupation var from superclass initialize


class Person
  def initialize(name, age, occupation)
    @name = name
    @age = age
    @occupation = occupation
  end
end

class Viking &lt; Person
  def initialize(name, age, occupation, weapon)
    super(name, age, occupation)
    @weapon = weapon
  end
end

eric = Viking.new(&quot;Eric&quot;, 24, 'pillager', 'broadsword')
# eric is now a pillager, but I don't want a Viking to have any @occupation
</code></pre>
<p>The question is either</p>
<ol>
<li><p>is it by design and I want to commit some Cardinal Sin against OOP principles?</p>
</li>
<li><p>how do I get it to work the way I want to (preferably without any crazy complicated metaprogramming techniques etc)?</p>
</li>
</ol>