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

5
  • This blog post was great when I was trying to understand it: Class and Instance Methods in Ruby Commented Oct 2, 2012 at 2:57
  • 2
    Uh oh, opening singleton class of a Class instance, are we? My head is spinning. Commented Oct 2, 2012 at 3: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 Commented Oct 2, 2012 at 3: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! Commented Oct 2, 2012 at 3:48
  • voted to close! it is weird when the OP cant vote to close all by himself though! Commented Oct 2, 2012 at 4:03

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.