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

  • Home
  • SEARCH
  • 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 592881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:47:11+00:00 2026-05-13T15:47:11+00:00

I’m trying to define a static variable and methods in a module that will

  • 0

I’m trying to define a static variable and methods in a module that will be extended/used by numerous classes. The following example demonstrates:

module Ammunition
  def self.included(base)    
    base.class_eval("@@ammo = [bullets]") 
  end

  def unload
    p @@ammo #<-- doesn't work
  end  
end

class Tank
  include Ammunition
  @@a += [shells]
end

class Airplane
  include Ammunition  
  @@a += [missiles, photon_torpedoes]
end

Tank.new.unload
Airplane.new.unload

This doesn’t work because ammunition doesn’t know how to evaluate @@ammo in the context of the class for some reason (I original thought the module would behave just like an include file). I would have to copy ‘unload’ to each class, which I’m doing right now, but I want to DRY it up b/c I have many other methods to add to the module.

Suggestions? The reasonable solution would be to evaluate ‘unload’ in the context of the class and not the module (but how to do this in Ruby?)

Thanks!

  • 1 1 Answer
  • 1 View
  • 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-13T15:47:11+00:00Added an answer on May 13, 2026 at 3:47 pm

    class variables can work strangely, and this use shows that off. What is the scope of @@ammo? Ammunition or does Tank have its own copy of it? It turns out that @@ammo is scoped by the module, and the classes that include it can simply access it.

    module Ammunition
      def self.included(base)    
        base.class_eval do
          puts "@@ammo was: #{defined?(@@ammo) ? @@ammo.join(',') : 'nil'}"
          @@ammo = ['bullets']
          puts "@@ammo is now: #{@@ammo}"
          puts '---'
        end
      end
    
      def unload
        @@ammo
      end  
    end
    
    class Tank
      include Ammunition
      @@ammo += ['shells']
    end
    
    class Airplane
      include Ammunition  
      @@ammo += ['missiles', 'photon_torpedoes']
    end
    
    puts "Tank unloaded: #{Tank.new.unload.join(', ')}"
    puts "Airplane unloaded: #{Airplane.new.unload.join(', ')}"
    

    This produces:

    @@ammo was: nil
    @@ammo is now: bullets
    ---
    @@ammo was: bullets,shells
    @@ammo is now: bullets
    ---
    Tank unloaded: bullets, missiles, photon_torpedoes
    Airplane unloaded: bullets, missiles, photon_torpedoes
    

    When Tank includes the module, it sets @@ammo from nil to an array with bullets in it. When Airplane includes the module, it overwrites the ammo value we just set.


    Here is what you want to do

    module Ammunition
      def self.included(base)    
        base.class_eval do
          include Ammunition::InstanceMethods
          extend  Ammunition::ClassMethods
          @ammo = ['bullets']
        end
      end
    
      module ClassMethods
        def ammo
          @ammo
        end
      end
    
      module InstanceMethods
        def unload
          self.class.ammo.join(',')
        end
      end
    end
    
    class Tank
      include Ammunition
      @ammo += ['shells']
    end
    
    class Airplane
      include Ammunition  
      @ammo += ['missiles', 'photon_torpedoes']
    end
    
    puts "Tank unloaded: #{Tank.new.unload}"
    puts "Airplane unloaded: #{Airplane.new.unload}"
    

    Classes can have instance variables, and their scope is easier to understand. And separating your module into instance and class methods allow you to provide functionality to both. This snippet generates the following output

    Tank unloaded: bullets,shells
    Airplane unloaded: bullets,missiles,photon_torpedoes
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 370k
  • Answers 370k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer In order to do this, you need to use dynamic… May 14, 2026 at 6:39 pm
  • Editorial Team
    Editorial Team added an answer Lucene is an inverted full-text index. This means that it… May 14, 2026 at 6:39 pm
  • Editorial Team
    Editorial Team added an answer I agree this may be premature optimization but if you… May 14, 2026 at 6:39 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.