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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:44:18+00:00 2026-05-26T13:44:18+00:00

How can I access instance variable from singleton method? class Test def initialize(a) @a

  • 0

How can I access instance variable from singleton method?

class Test
  def initialize(a)
    @a = a
  end

  def item
    item = "hola"
    def item.singl
      [self, @a].join(" + ")
    end
    item
  end
end

test = Test.new("cao")
item = test.item
item.singl
#=> ... @a is nil
  • 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-26T13:44:19+00:00Added an answer on May 26, 2026 at 1:44 pm

    Try using define_method. Def puts you inside a new scope.

    class Test
      def initialize(a)
        @a = a
      end
    
      def item
        item = "hola"
        item.singleton_class.send(:define_method, :singl) do
          [self, @a].join(" + ")
        end
    
        item
      end
    end
    
    test = Test.new("cao")
    item = test.item
    item.singl #=> "hola + "
    

    In your example though, you still have a problem, inside the singleton class of a string @a hasn’t been defined. This is primarily because self in this context is the string instance, not a test instance where @a exists. To fix this you can rebind the instance variable to something else, but this might not be the behavior you’re looking for. You can also, set the instance variable in your new singleton class.

    For example,

    Rebind the variable

    class Test
      def initialize(a)
        @a = a
      end
    
      def item
        item = "hola"
        new_self = self
        item.singleton_class.send(:define_method, :singl) do
          [self, new_self.instance_variable_get(:@a)].join(" + ")
        end
    
        item
      end
    end
    
    test = Test.new("cao")
    item = test.item
    item.singl
    

    Set a instance string variable

    class Test
      def initialize(a)
        @a = a
      end
    
      def item
        item = "hola"
        item.singleton_class.send(:define_method, :singl) do
          [self, @a].join(" + ")
        end
    
        item.singleton_class.send(:define_method, :set) do
          @a = "cao"
        end
    
        item
      end
    end
    
    test = Test.new("cao")
    item = test.item
    item.set
    item.singl
    

    Its important to note the differences between the two methods. In the first method, we retain a reference to the original instance variable, via the original object. In the second method, we make a new instance variable, bound under the new singleton class, containing a copy of the original test.@a.

    If you are using a non-native object you may be able to get away with a mixture of both methods. Referencing the old instance variable’s object with the singelton classes new instance variable via a pointer, but this won’t work for int, string, float, etc…

    EDIT: As Benoit pointed out, in the second method the “set” method should just be an attr_accessor. In fact, you can set the instance variable without defining a new method at all. Via item.instance_variable_set(:@, "cao")

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

Sidebar

Related Questions

How can I use a class instance variable as an argument for a method
How can I access a controller instance from the view? E.g. I have a
How can hibernate can access a private field/method of a java class , for
I want to make an instance variable that can't be accessed from outside. Is
Is there any way I can access Private member variable of a class? Editing:
I can access the database either from a .NET program (using ODBC) or through
I know you can access the Contact Store from the SDk, but is it
Trying to understand how CoffeeScript instance and class variable works I came with this
If a variable is declared as public static varName; , then I can access
I can access Spring beans in my Servlets using WebApplicationContext springContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext()); in

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.