Module Mixin & Class Variables

Discussion in 'Ruby' started by JP Billaud, Mar 6, 2011.

  1. JP Billaud

    JP Billaud Guest

    Hey,

    I would like to understand better what happens when one includes a
    module with class variables inside a class.

    ===

    module MyModule

    @@test = "test"

    def self.my_test
    puts "From Module"
    puts @@test.object_id
    end
    end

    class B
    include MyModule

    def self.my_test
    puts "From Class"
    puts @@test.object_id
    end
    end

    B.my_test
    MyModule.my_test

    > From Class
    > -607833218
    > From Module
    > -607833218


    ===

    Doing so, the output shows that in both cases the @@test object is the
    same. Does someone have the big picture about class variables look up
    while mixin a module inside a class?

    Thanks,

    --
    Posted via http://www.ruby-forum.com/.
    JP Billaud, Mar 6, 2011
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben
    Replies:
    3
    Views:
    216
  2. Farrel Lifson

    Mixin a module method as a class method

    Farrel Lifson, Oct 18, 2006, in forum: Ruby
    Replies:
    9
    Views:
    113
    Trans
    Oct 19, 2006
  3. Sijo Kg

    class, Module and Mixin

    Sijo Kg, Sep 24, 2009, in forum: Ruby
    Replies:
    1
    Views:
    106
    Robert Klemme
    Sep 24, 2009
  4. John Lane
    Replies:
    6
    Views:
    171
    John Lane
    Feb 9, 2010
  5. Konrad K.
    Replies:
    6
    Views:
    128
    Phillip Gawlowski
    Jan 5, 2011
Loading...

Share This Page