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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:45:44+00:00 2026-05-22T20:45:44+00:00

I want to be able to make an option passed to my class method

  • 0

I want to be able to make an option passed to my class method (auditable) available to instance methods. I’m mixing in both the class and instance methods using a Module.

The obvious choice is to use a class variable, but I get an error when trying access it:

uninitialized class variable @@auditable_only_once in Auditable

class Document
  include Auditable
  auditable :only_once => true
end

# The mixin
module Auditable
  def self.included(base)
    base.extend(ClassMethods)
  end

  module ClassMethods
    def auditable(options = {})

      options[:only_once] ||= false

      class_eval do
        # SET THE OPTION HERE!!
        @@auditable_only_once = options[:only_once]
      end
      end
    end

    private

    def audit(action)
      # AND READ IT BACK LATER HERE
      return if @@auditable_only_once && self.audit_item
      AuditItem.create(:auditable => self, :tag => "#{self.class.to_s}_#{action}".downcase, :user => self.student)
    end    
  end

I’ve stripped out some of the code to make this a bit easier to read, the full code is here: https://gist.github.com/1004399 (EDIT: Gist now includes the solution)

  • 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-22T20:45:45+00:00Added an answer on May 22, 2026 at 8:45 pm

    Using @@ class instance variables is irregular and the number of of occasions when they’re strictly required is exceedingly rare. Most of the time they just seem to cause trouble or confusion. Generally you can use regular instance variables in the class context without issue.

    What you might want to do is use a different template for this sort of thing. If you have mattr_accessor, which is provided by ActiveSupport, you may want to use that instead of that variable, or you can always write your own equivalent in your ClassMethods component.

    One approach I’ve used is to break up your extension into two modules, a hook and an implementation. The hook only adds the methods to the base class that can be used to add the rest of the methods if required, but otherwise doesn’t pollute the namespace:

    module ClassExtender
      def self.included(base)
        base.send(:extend, self)
      end
    
      def engage(options = { })
        extend ClassExtenderMethods::ClassMethods
        include ClassExtenderMethods::InstanceMethods
    
        self.class_extender_options.merge!(options)
      end
    end
    

    This engage method can be called anything you like, as in your example it is auditable.

    Next you create a container module for the class and instance methods that the extension adds when it is exercised:

    module ClassExtenderMethods
      module ClassMethods
        def class_extender_options
          @class_extender_options ||= {
            :default_false => false
          }
        end
      end
    
      module InstanceMethods
        def instance_method_example
          :example
        end
      end
    end
    

    In this case there is a simple method class_extender_options that can be used to query or modify the options for a particular class. This avoids having to use the instance variable directly. An example instance method is also added.

    You can define a simple example:

    class Foo
      include ClassExtender
    
      engage(:true => true)
    end
    

    Then test that it is working properly:

    Foo.class_extender_options
    # => {:default_false=>false, :true=>true}
    
    foo = Foo.new
    foo.instance_method_example
    # => :example
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to be able to make a line graph using GTK+ but I'm
I want to be able to make an HTTP call updating some select boxes
I create a file in JavaScript. Now I want to be able to make
I want the user to be able to make some preferences like colors, prefered
I'm using LaTeX and BibTeX for an article, and I want to able to
Want to be able to provide a search interface for a collection of objects
I want to be able to capture the exception that is thrown when a
I want to be able to do: For Each thing In things End For
I want to be able to play sound files in my program. Where should
I want to be able to view a SQL Server 2005 Reporting Services report

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.