1

Possible Duplicate:
Difference between class variables and class instance variables?

I would like to understand the difference here:

class Blog
  class << self
    def tags (default_tags)
      @tags ||= default_tags
    end
  end
end

Now, Blog.tags always return what was initially assigned. Since I am playing around with Rails, that is the case for every request.

the @tags above is class level instance variable and seem to act similar to @@tags, which is class variable. Are they in fact same? is there any difference one should be aware of between the two, and to decide which one to chose over the other?

Or am I completely wrong in my understanding?

ps: This is apparently a made up example

Community
  • 1
  • 1
thanikkal
  • 3,326
  • 3
  • 27
  • 45
  • This blog post was great when I was trying to understand it: [Class and Instance Methods in Ruby](http://railstips.org/blog/archives/2009/05/11/class-and-instance-methods-in-ruby/) – Josh Voigts Oct 02 '12 at 02:57
  • 2
    Uh oh, opening singleton class of a Class instance, are we? My head is spinning. – Boris Stitnicky Oct 02 '12 at 03:01
  • Boris,essentially a noob question this. :) Saw similar thing is done is one of the gems i was looking to use, and wanted to understand it better – thanikkal Oct 02 '12 at 03:44
  • Thanks for pointing that Andrew. Yes it is duplicate of the one you mentioned. Now I understand the difference is whether the variable is shared with the derived classesor not. Thanks! – thanikkal Oct 02 '12 at 03:48
  • voted to close! it is weird when the OP cant vote to close all by himself though! – thanikkal Oct 02 '12 at 04:03

0 Answers0