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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:29:21+00:00 2026-06-04T13:29:21+00:00

So I am trying out mixins and some metaprogramming in ruby and can’t get

  • 0

So I am trying out mixins and some metaprogramming in ruby and can’t get this to work for me. I want it to print “Baboon”

 module A

  def self.included(base)
      base.extend ClassMethods
  end

  module ClassMethods
    def install_A
        include InstanceMethods
    end
  end

  module InstanceMethods
      class B
         def testB
           #What goes here???
           A.monkey
         end
      end

      attr_accessor :monkey

      def initialize()
         @monkey = "baboon"
      end

      def test
          b = B.new
          puts b.testB
      end
  end
end

class Chimp
  include A
  install_A
end

c = Chimp.new
c.test
  • 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-04T13:29:23+00:00Added an answer on June 4, 2026 at 1:29 pm

    B is its own self-contained class. It is not associated or connected with any of the other classes or modules except to say that an instance of class B happens to be created inside of InstanceMethods::test. Declaring class B inside of the definition for module InstanceMethods limits the scope of B such that it’s not visible outside of InstanceMethods, but it doesn’t connect B and InstanceMethods in any way.

    What you need is to make the module’s variable visible inside B. You can implement an initialize method for B that takes a parameter, and InstanceMethods::test can use b = B.new(@monkey) to pass the value to B (make sure B::initialize stores the value as an instance variable).

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

Sidebar

Related Questions

been trying out this for quite some time but I'm still unable to built
Trying out some things with php and html, I'm having issues trying to get
I was trying to figure out how mixins are defined in Maven 3, but
Trying out the examples found on pleac.sf.net , I'm not able to get an
So i'm trying out MVC after only playing with it some time ago. Need
Im trying out some iPhone scroll view tests, but when i load the view,
For trying out some pointer stuff, I made an instance variable in an existing
Am trying out Ruby on Rails in Windows XP using the Hello World example.
Trying out VS 2010, but still want to target .NET 3.5. Have all my
Just trying out ubuntu server on my pc and have been testing some commands

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.