Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>So you need to learn the rules of ruby scoping.</p> <p>Ruby variables have different scope, depending on their prefix:</p> <ul> <li><code>$global_variables</code> start with a <code>$</code>, and are available to everyone.</li> <li><code>@instance_variables</code> start with a single <code>@</code>, and are stored with the current value of <code>self</code>. If two scopes share the same value of <code>self</code> (they're both instance methods, for example), then both share the same instance variables</li> <li><code>@@class_variable</code> start with <code>@@</code>, and are stored with the class. They're shared between all instances of a class - and all instances of subclasses of that class.</li> <li><code>Constants</code> start with a capital letter, and may be all caps. Like class variables, they're stored with the current <code>self.class</code>, but they also trickle up the hierarchy - so if you have a class defined in a module, the instances of the class can access the module's constants as well. Constants defined outside of a class have global scope. Note that a constant variable means that which object is bound to the constant won't change, not that the object itself won't change internal state.</li> <li><code>local_variables</code> start with a lowercase letter</li> </ul> <p>You can read more about scope <a href="http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html#UP" rel="nofollow noreferrer">here</a>.</p> <p>Local variables scoping rules are mainly standard - they're available in all subscopes of the one in which they are defined <code>except</code> when we move into a module, class, or method definition. So if we look at your code from your answer</p> <pre><code>class TestDevice attr_accessor :loghash def initialize @loghash = { } end end device = TestDevice.new class Somethingelse def self.something device.loghash='something here' # doesn't work end end </code></pre> <p>The scope of the <code>device</code> local variable defined at the toplevel does not include the <code>Somethingelse.something</code> method definition. So the <code>device</code> local variable used in the <code>Somethingelse.something</code> method definition is a different (empty) variable. If you want the scoping to work that way, you should use a constant or a global variable.</p> <pre><code>class TestDevice attr_accessor :loghash def initialize @loghash = { } end end DEVICE = TestDevice.new $has_logged = false class Somethingelse def self.something DEVICE.loghash='something here' $has_logged = true end end p DEVICE.loghash # prints `{}` p $has_logged # prints `false` Somethingelse.something p DEVICE.loghash # prints `"something here"` p $has_logged # prints `true` </code></pre>
 

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