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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:12:38+00:00 2026-05-27T16:12:38+00:00

I play with metaprogramming in ruby and I have a question. I have a

  • 0

I play with metaprogramming in ruby and I have a question. I have a class:

class Klass
  class << self
    @x = "yeah"
  end
end
b = Klass.new
a = class << Klass; self; end
a.instance_eval "@x"           #=> yeah
Klass.instance_eval "@x"       #=> nil

Why? In variable a I have a singleton class, right? And Klass.instance_eval exec in context of a singleton class:

Klass.instance_eval "def yeah; puts 10; end"
Klass.yeah                     #=> 10

Also, Klass in interpreter points to context of class, yes? And a points to context of a singleton class?
And which indicates a.class_eval and a.instance_eval? I do:

a.instance_eval "def pops; puts 0; end"
a.class_eval "def popsx; puts 1; end"
a.pops                         #=> 0
a.popsx                        # FAIL
Klass.pops                     # FAIL
Klass.popsx                    #=> 1
b.pops; b.popsx                # DOUBLE FAIL

and I do not understand this. Thanks!

  • 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-27T16:12:39+00:00Added an answer on May 27, 2026 at 4:12 pm

    First, while it seems like eigentclass is used by some people singleton class is more common term. Singleton class contains object-specific behavior for an object in Ruby. You can’t create other instances of that class except the original object this singleton class belongs to.

    Speaking about defining of methods inside different types of eval this article introduces nice rule for methods defined in instance_eval and class_eval:

    Use ClassName.instance_eval to define class methods.
    Use ClassName.class_eval to define instance methods.
    

    That pretty much describes the situation.

    There was a huge write-up about classes that are instances of Class class, their singleton classes that are subclasses of Class class and some other crazy stuff (not that much related to the problem). But as your question can be easily applied to regular objects and their classes (and it makes things much easier to explain), I decided to remove that all (though, you can still see that stuff in revisions history of the answer).

    Let’s look at regular class and instance of that class and see how that all works:

    class A; end
    a = A.new
    

    Method definitions inside different types of eval:

    # define instance method inside class context
    A.class_eval { def bar; 'bar'; end }
    puts a.bar     # => bar
    puts A.new.bar # => bar
    
    # class_eval is equivalent to re-opening the class
    class A
      def bar2; 'bar2'; end
    end
    puts a.bar2     # => bar2
    puts A.new.bar2 # => bar2
    

    Defining object-specific methods:

    # define object-specific method in the context of object itself
    a.instance_eval { def foo; 'foo'; end }
    puts a.foo # => foo
    
    # method definition inside instance_eval is equivalent to this
    def a.foo2; 'foo2'; end
    puts a.foo2 # => foo2
    
    # no foo method here
    # puts A.new.foo # => undefined method `foo' for #<A:0x8b35b20>
    

    Let’s now look at singleton class of object a:

    # singleton class of a is subclass of A
    p (class << a; self; end).ancestors
    # => [A, Object, Kernel, BasicObject]
    
    # define instance method inside a's singleton class context
    class << a
      def foobar; 'foobar'; end;
    end
    puts a.foobar # => foobar
    
    # as expected foobar is not available for other instances of class A
    # because it's instance method of a's singleton class and a is the only
    # instance of that class
    # puts A.new.foobar # => undefined method `foobar' for #<A:0x8b35b20>
    
    # same for equivalent class_eval version
    (class << a; self; end).class_eval do
      def foobar2; 'foobar2'; end;
    end
    puts a.foobar2 # => foobar2
    
    # no foobar2 here as well
    # puts A.new.foobar2 # => undefined method `foobar2' for #<A:0x8b35b20>
    

    Now let’s look at instance variables:

    # define instance variable for object a
    a.instance_eval { @x = 1 }
    
    # we can access that @x using same instance_eval
    puts a.instance_eval { @x } # => 1
    # or via convenient instance_variable_get method
    puts a.instance_variable_get(:@x) # => 1
    

    And now to instance variables inside class_eval:

    # class_eval is instance method of Module class
    # so it's not available for object a
    # a.class_eval { } # => undefined method `class_eval' for #<A:0x8fbaa74>
    
    # instance variable definition works the same inside
    # class_eval and instance_eval
    A.instance_eval { @y = 1 }
    A.class_eval    { @z = 1 }
    
    # both variables belong to A class itself
    p A.instance_variables # => [:@y, :@z]
    
    # instance variables can be accessed in both ways as well
    puts A.instance_eval { @y } # => 1
    puts A.class_eval    { @z } # => 1
    
    # no instance_variables here
    p A.new.instance_variables # => []
    

    Now if you replace class A with class Class and object a with object Klass (that in this particular situation is nothing more than instance of class Class) I hope you’ll get explanation to your questions. If you still have some feel free to ask.

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

Sidebar

Related Questions

I play around with arrays and hashes quite a lot in ruby and end
I need to play .vox files on my web page. Does anyone have any
I have started to play a little with Qt 4. And then I have
Started to play around with Jenkins. Created new job and entered SVN repository URL.
I want to play YouTube videos from my iPhone app. I have to tried
I play around a lot when I'm programming in FLEX, mostly cause I'm new
I play to self-study 6.001 with the video lectures and lecture handouts. However, I
Using play framework 1.2.4 with scala. I have few play jobs that looks like
a play on a similar question asked by me that was graciously answered. three
I have a simple play app. All of it's actions render to views. About

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.