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 8151765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:29:38+00:00 2026-06-06T15:29:38+00:00

I am trying to access instance variables I have defined in RSpec in the

  • 0

I am trying to access instance variables I have defined in RSpec in the modules that I am mixing into RSpec, but I can’t seem to get this to work.

A simplifed spec file shows the problem I am having:

my_spec.rb

require 'rspec'

describe 'passing instance variables from specs into ruby mixins' do
  it 'should pass the instance variable to the module' do
    @a = 'a'
    A.a.should == 'a'
  end

  it 'should pass the instance variable to the module in the module' do
    @b = 'b'
    A.b.should == 'b'
  end

  it 'should pass instance varisables from one module to the other' do
    A.c.should == 'c'
  end

end

module B
  def b
    return @b
  end

  def c
    return @c
  end
end

module A
  extend B
  @c = 'c'
  def self.a
    return @a
  end
end

Results:

1) passing instance variables from specs into ruby mixins should pass the instance variable to the module
     Failure/Error: A.a.should == 'a'
       expected: "a"
            got: nil (using ==)
     # ./spec/my_spec.rb:6:in `block (2 levels) in <top (required)>'

  2) passing instance variables from specs into ruby mixins should pass the instance variable to the module in the module
     Failure/Error: A.b.should == 'b'
       expected: "b"
            got: nil (using ==)
     # ./spec/my_spec.rb:11:in `block (2 levels) in <top (required)>'

Basically, I want to be able to access the instance variables @a, @b in both the modules A and B. I have tried using class variables @@a and @@b, but this doesn’t work.

I can use global variables ($a and $b), and this works, but I feel this isn’t elegant as they’re global variables, which are evil.

Working code:

require 'rspec'

describe 'passing instance variables from specs into ruby mixins' do
  it 'should pass the instance variable to the module' do
    $a = 'a'
    A.a.should == 'a'
  end

  it 'should pass the instance variable to the module in the module' do
    $b = 'b'
    A.b.should == 'b'
  end

  it 'should pass instance varisables from one module to the other' do
    A.c.should == 'c'
  end

end

module B
  def b
    return $b
  end

  def c
    return @c
  end
end

module A
  extend B
  @c = 'c'
  def self.a
    return $a
  end
end

Any ideas?

  • 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-06-06T15:29:40+00:00Added an answer on June 6, 2026 at 3:29 pm

    Even though they share the same name, they are scoped separately, because they are scoped to the instance where they are defined.

    That is to say, the instance variables you’ve set in the specs only exist within the scope of those specs. Similarly, the instance variables inside the modules are similarly scoped to that context.

    I’m not sure if it matches what you’re trying to accomplish given the example is abstracted, but try this:

    require 'rspec'
    
    module B
      def b= b
        @b = b
      end
    
      def b
        return @b
      end
    
      def c= c
        @c = c
      end
    
      def c
        return @c
      end
    end
    
    module A
      extend B
    
      @c = 'c'
    
      def self.a= a
        @a = a
      end
    
      def self.a
        return @a
      end
    end
    
    describe 'passing instance variables from specs into ruby mixins' do
      it 'should pass the instance variable to the module' do
        A.a = 'a'
        A.a.should == 'a'
      end
    
      it 'should pass the instance variable to the module in the module' do
        A.b = 'b'
        A.b.should == 'b'
      end
    
      it 'should pass instance varisables from one module to the other' do
        A.c.should == 'c'
      end
    end
    

    This could then be simplified to use attr_accessor instead of defining the getter/setter methods manually.

    The problem is that you’re then simply testing the guts of Ruby.

    Did I misunderstand the problem you’re trying to solve?

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

Sidebar

Related Questions

I'm trying to access a member structs variables, but I can't seem to get
I am trying to access the singleton instance created by my WCF service but
I'm running into some type of a scope issue that's preventing instance variables from
I'm trying to access instance variables from within a nested object ('action'). The only
I am trying to access an instance variable which is set in the controller
I'm trying to access my Tomcat instance on OSX with Windows 7 via VirtualBox.
I'm trying to access list data using SSRS 2008. I have created an XML
I am inside a Rails controller and I am trying to access my instance
I have a class that I am trying to do unit tests on. The
I am trying to have a private instance variable and use a getter method

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.