Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's your problem:</p> <pre><code>person_array = Array.new(number_of_people, Person) </code></pre> <p>In short, don't make array like this. Use the <code>[]</code> literal syntax. What this returns is:</p> <pre><code>[Person, Person, Person] </code></pre> <p>That is 3 references to the <code>Person</code> class, not instances. Then later you do:</p> <pre><code>person_array.push(Person.new(iq, speed, power, beauty)) </code></pre> <p>And you end up with:</p> <pre><code>[Person, Person, Person, person_instance] </code></pre> <p>So when you iterate through and call <code>send_for_beer</code> on that first item, it does have that method because <code>send_for_beer</code> is an instance method that you are calling erroneously on a class object.</p> <p>The fix here is to simply assign <code>person_array</code> to an empty array literal, and then push things to it.</p> <pre><code>person_array = [] </code></pre> <hr> <p>And a minor style note: <code>&lt;&lt;</code> is usually preferred to <code>Array#push</code>, making the filling of the array look more like this.</p> <pre><code>person_array &lt;&lt; Person.new(iq, speed, power, beauty) </code></pre> <hr> <p>Ruby also support implicit return of the last expression in a method. So you do not need to <code>return result</code>. Instead, simply calulate the return value as the only line in the method.</p> <pre><code>def send_for_beer @iq * 2 + @speed * 10 + @power * 5 + @beauty end </code></pre> <hr> <p>Instance variables don't quite work like that either. When you have <code>@name</code> in the class body directly, you are not initializing instance variables for each instance. You are actually setting instance variable on the class object (which is weird, I know). What you actually need to do is set them from any instance method, typically <code>initialize</code>, which you are doing here. So you can totally remove the instance variable setting at the class level here.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload