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

  • Home
  • SEARCH
  • 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 1005005
In Process

The Archive Base Latest Questions

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

So I was doing a refresher on Ruby and I saw this guy’s blog

  • 0

So I was doing a refresher on Ruby and I saw this guy’s blog about creating class-level instance variable in Ruby. I am still trying to understand what the code actually does here. His blog can be found here

http://railstips.org/blog/archives/2006/11/18/class-and-instance-variables-in-ruby/

and I have created a simple code based on his example to show what I am trying to understand

class Polygon
  class << self; attr_accessor :sides end
  @sides = 10
  def initialize
  end
end

class Triangle < Polygon
  @sides = 3
  class << self; attr_accessor :sides end
  def initialize
  end
end

puts Triangle.sides #3
puts Polygon.sides #10

So the line that I really want to understand is (probably you guys have guessed it),

class << self; attr_accessor :sides end

What does this really do? what is he appending self to class ? is class an array then? Please elaborate as much as you can. Thank you.

  • 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-16T08:13:23+00:00Added an answer on May 16, 2026 at 8:13 am

    The << is not a method (that is not exclusive to Array BTW), but is the syntax for defining a metaclass

    Basically, a metaclass is THE class of a single object (some people calls them singleton classes). For example, if you define

    x = Foo.new
    y = Foo.new
    class << x
      def quack
        "Quack!"
      end
    end
    

    then calling x.quack will return “Quack”, but y.quack will throw a NoMethodError. So, the code is only evaluated on x’s metaclass.

    But… classes are objects too, right? So, when you evaluate that line, it is the equivalent of doing

    class << Triangle
      attr_accessor :sites
    end
    

    which will just define an instance variable in the metaclass of Triangle. This is, the Triange class, which is an object too, will have an instance variable called sides

    More info in this and this links. Once you get the idea, go to the nearest irb console and experiment with that.

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

Sidebar

Related Questions

Doing some homework here (second assignment, still extremely green...). The object is to read
I am doing a refresher on CS fundamentals. I have a question on linked
I'm doing this in an AsyncTask, everything working fine but my view is not
I might be going about this in totally the wrong way but this is
I am doing a refresher an algorithms and data structures. I am confused on
i am doing an ajax call and i refresh a partial view. Inside the
Doing some ETL and I have a wide file with lots of cols, for
Doing odd/even styling with jQuery is pretty easy: $(function() { $(.oddeven tbody tr:odd).addClass(odd); $(.oddeven
Doing an ajax get request works as expected using the following code: $.ajax({ type:
Doing my first SL4 MVVM RIA based application and i ran into the following

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.