Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>99% of the time I would recommend against removing an instance variable, because it's extra code for no extra benefit.</p> <p>When you're writing code, generally you're trying to solve a real-world problem. With the instance variable, some questions to ask are:</p> <ul> <li>What real world concept am I trying to model with the various states the variable can be in?</li> <li>What am I going to do with the values stored in the variable?</li> </ul> <p>If you're just trying to blank out the continent value stored in a <code>World</code> object, you can set <code>@continent</code> to <code>nil</code> as dustmachine says. This will work fine for the 99% of the cases. (Accessing a removed instance variable will just return <code>nil</code> anyway.)</p> <p>The only possible case (I can think of) when removing the instance variable could be useful is when you're caching a value that may be <code>nil</code>. For example:</p> <pre><code>class Player def score(force_reload = false) if force_reload # purge cached value remove_instance_variable(:@score) end # Calling 'defined?' on an instance variable will return false if the variable # has never been set, or has been removed via force_reload. if not defined? @score # Set cached value. # Next time around, we'll just return the @score without recalculating. @score = get_score_via_expensive_calculation() end return @score end private def get_score_via_expensive_calculation if play_count.zero? return nil else # expensive calculation here return result end end end </code></pre> <p>Since <code>nil</code> is a meaningful value for <code>@score</code>, we can't use <code>nil</code> to indicate that the value hasn't been cached yet. So we use the undefined state to tell us whether we need to recalculate the cached value. So there are 3 states for <code>@score</code>:</p> <ol> <li><code>nil</code> (means user has not played any games)</li> <li>number (means user played at least once but did not accrue any points)</li> <li>undefined (means we haven't fetched the calculated score for the Player object yet).</li> </ol> <p>Now it's true that you could use another value that's not a number instead of the undefined state (a symbol like <code>:unset</code> for example), but this is just a contrived example to demonstrate the idea. There are cases when your variable may hold an object of unknown type.</p>
    singulars
    1. This table or related slice is empty.
    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. This table or related slice is empty.
    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