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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:28:55+00:00 2026-06-11T07:28:55+00:00

In this code block, @@y = 1 class MyClass @@y = 2 end p

  • 0

In this code block,

@@y = 1
class MyClass
 @@y = 2
end
p @@y # => 2

naively, it seems that @@y is in top-level scope, and it isn’t the same @@y as the one in MyClass‘s scope. Why is @@y affected by the class MyClass definition? (why is the result 2?)

  • 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-06-11T07:28:57+00:00Added an answer on June 11, 2026 at 7:28 am

    Let’s look at this example. Here @@x in Bar is indeed separate from @@x in Foo.

    class Foo
      @@x = 1
    end
    
    class Bar
      @@x = 2
    end
    
    Foo.class_variable_get(:@@x) # => 1
    Bar.class_variable_get(:@@x) # => 2
    

    But what happens if Bar is a child of Foo?

    class Foo
      @@x = 1
    end
    
    class Bar < Foo
      @@x = 2
    end
    
    Foo.class_variable_get(:@@x) # => 2
    Bar.class_variable_get(:@@x) # => 2
    

    In this case, @@x is the same in both cases, and it is the one declared in Foo.

    Now, back to your example:

    @@y = 1
    class MyClass
      @@y = 2
    end
    p @@y
    

    The first line declares class variable in the root scope. Root is a special object main which is of type Object. So, essentially, you’re defining a class variable on Object class. Since everything is an Object, this is how definition of MyClass also inherits @@y and is able to change it.

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

Sidebar

Related Questions

I'm using EclEmma for coverage analysis. My Java code includes a synchronized(MyClass.class) {} block.
So I run this Windows Server 2008 security update and this code block is
This code only renders a dodecahedron and completely ignores the glBegin(GL_TRIANGLES) block: glutSolidDodecahedron(); glBegin(GL_TRIANGLES);
I want to block special character in this code. In other words, user can
Can anyone explain why this code: {% form_theme form _self %} {% block avo_gallery_upload_widget
This should be super simple. The first code block works, but when I wrap
Following leads in this thread and others, I've set up a code block where
I'm using Drupal 7. And i created a block and embed this code: $contents
For this block of code: if(!skipit) { var update_argument=''; if (document.formname.fieldname) { update_argument=document.formname.fieldname[document.formname.fieldname.selectedIndex].value; }
I have this block of code inside a loop: var points = [new google.maps.LatLng(lat1,

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.