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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:14:16+00:00 2026-05-22T00:14:16+00:00

When I call self.class.instance_variable_set(@var, …) from inside a class method, where is that variable

  • 0

When I call self.class.instance_variable_set("@var", ...) from inside a class method, where is that variable actually stored? Is it on the class itself? On the instance of that class? I can’t seem to find it with any of the following:

e = Example.new
e.instance_variables
e.class.class_variables

I even tried using the (class << self; self; end) trick but I can’t find anything (http://ruby-metaprogramming.rubylearning.com/html/seeingMetaclassesClearly.html).

Here is the code snippet (which works as I need) but I’m not sure why it works 🙂

module HandyModule
  def self.included(base)
    base.extend ClassMethods
  end
  module ClassMethods
    def use_template(template_name)
      self.class.instance_variable_set("@_template", template_name)
    end
    def build(attributes = {})
      template_name = self.class.instance_variable_get("@_template")
      # do stuff with the template
    end
  end
end

class Example
  include HandyModule
  use_template :contact_form
end

Essentially I can include this handy module, and then I have a class method called use_template with which I can specify which template to be used by my build method.

  • 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-22T00:14:17+00:00Added an answer on May 22, 2026 at 12:14 am

    When you call use_template inside the class definition, the self is the class Example. When you call self.class, it is Example.class, or Class. You define the instance variable to the class of the classes.

    class Class
      p @_template
    end
    # prints :contact_form
    

    You probably should use just self instead of self.class.

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

Sidebar

Related Questions

How do I call shell commands from inside of a Ruby program? How do
Hopefully the title is self explanatory, what is the advantage of using the .call()
If I call finalize() on an object from my program code, will the JVM
I want to call a function from a .NET DLL (coded in C#) from
I would like to initialize a class, then call an attribute which was set
Can I define a Python method to be both static and instance at the
Call me a 'n00b', but I am new to creating Script Controls. I want
I call my JavaScript function. Why do I sometimes get the error 'myFunction is
I'm trying to call an Antlr task in my Ant build.xml as follows: <path
I need to call into a Win32 API to get a series of strings,

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.