Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Modules add functions to things; <code>attr_accessor</code> adds functions for interacting with a variable.</p> <pre><code>module ToMix @module_var = "module_var" attr_accessor :mixed_var def initialize @mixed_var = "mixed_var" end def mix_function puts "mix_function: #{@mixed_var}" end def self.module_function @module_var end end class Mixed include ToMix end Mixed.new.mixed_var </code></pre> <p>Notably,</p> <pre><code>"".extend(ToMix).mixed_var == nil # no error, but no value, interesting! </code></pre> <p>but</p> <pre><code>(a = "".extend(ToMix)).mixed_var = "interesting" a.mixed_var == "interesting" </code></pre> <p>and</p> <pre><code>ToMix.module_function == "module_var" </code></pre> <p><a href="https://stackoverflow.com/a/151774/171916">https://stackoverflow.com/a/151774/171916</a> <a href="http://www.natontesting.com/2009/09/28/accessing-instance-variables-declared-in-ruby-modules/" rel="nofollow noreferrer">http://www.natontesting.com/2009/09/28/accessing-instance-variables-declared-in-ruby-modules/</a> <a href="https://stackoverflow.com/questions/3127069/how-to-dynamically-alter-inheritance-in-ruby">How to dynamically alter inheritance in Ruby</a></p> <p>Edit: Those wiser than I should correct me if I'm wrong, but module and class definitions are themselves objects. Defining a <code>@var</code> in a module definition thus adds the var to the module object itself</p> <p>Edit: Those wiser than I have corrected me: While class and module definitions sort of behave like a singleton, they are not themselves objects. You can think of <code>def self.bacon</code> and <code>@var</code> outside of methods as c++ static methods and variables, but they can /only/ be access like a static, even if you're in an instance.</p>
 

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