Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7547253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:24:19+00:00 2026-05-30T09:24:19+00:00

I want to know how the instance variables of a Ruby module behaves across

  • 0

I want to know how the instance variables of a Ruby module behaves across multiple classes which ‘mix’ it ‘in’. I wrote a sample code to test it:

# Here is a module I created with one instance variable and two instance methods.
module SharedVar
  @color = 'red'
  def change_color(new_color)
    @color = new_color
  end
  def show_color
    puts @color
  end
end

class Example1
  include SharedVar
  def initialize(name)
    @name     = name
  end
end

class Example2
  include SharedVar
  def initialize(name)
    @name     = name
  end
end

ex1 = Example1.new("Bicylops")
ex2 = Example2.new("Cool")

# There is neither output or complains about the following method call.
ex1.show_color
ex1.change_color('black')
ex2.show_color

Why it doesn’t work? And Could someone explain what will the actual behavior of @color across multiple Example$ instances?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T09:24:20+00:00Added an answer on May 30, 2026 at 9:24 am

    In Ruby modules and classes are objects, so it’s possible to set instance variables for them.

    module Test
      @test = 'red'
      def self.print_test
        puts @test
      end
    end
    
    Test.print_test #=> red
    

    Your mistake is thinking that the variable @color is the same for:

    module SharedVar
      @color
    end
    

    and

    module SharedVar
      def show_color
        @color
      end
    end
    

    which is not.

    In the first example, the instance variable belongs to the SharedVar object and in the second example the instance variable belongs to the object you include the module in.

    Another explanation by self pointer. In the first example the self pointer is set to the module object SharedVar, so typing @color will refer to the object SharedVar and there’s no connection with another object. In the second example, the method show_color can be called only on some object, i.e. ex1.show_color, so the self pointer will point to ex1 object. So in this case the instance variable will refer to ex1 object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to know the ideal instance site for a portal, may it be
I want to know what happens when I create an instance of a ServiceHost
want to know why String behaves like value type while using ==. String s1
I want to know which tool can be used to measure the cyclomatic complexity
I want to know how to use variables for objects and function names in
I want to know if i can create a custom google maps application,on which
i want to do the following: I want to declare the instance variables of
In ruby, I want to create a method on a class which will save
Something is happening with one of my class's instance variables. I want to make
Want to know what the stackoverflow community feels about the various free and non-free

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.