class variables have a niche to fill: visibility to a class and its instances, and to no one else. Typically, this means being visible in class-method definitions and instancemethod definitions, and sometimes at the top level of the class definition.

I understood that. But didn't find any example of the part - sometimes at the top level of the class definition.

Could anyone help me here with example what it means by?

Member Avatar for LastMitch

Can class variable be accessible to the top level of the class definition?

If you want to find that error then post the code that is related to it. Which platform are you using?

Could anyone help me here with example what it means by?

You have to post the code where the error appears. I mean for you understanding the error is good but I feel you want to debug this issue.

@LastMitch - thanks for your reply. But no error I have found. I wanted to see one example against the bold marked statement in my description. I don't have any valid example to digest that statement too.

Here's one example of a class variable being used at the top level of the class definition:

class MyClass
  @@number_of_instances = 0

  def self.number_of_instances
    @@number_of_instances
  end

  def initialize
    @@number_of_instances += 1
  end
end

As you can see, the class variable @@number_of_instances is used in 3 places here: it is read in the class method number_of_instances, it is incremented in the instance method initialize and it is initialized to 0 at the top level of the class definition.

@LastMitch:

You want to see how the error looks like?

What error? No one mentioned any error except you.

Member Avatar for LastMitch

What error? No one mentioned any error except you.

What is your problem?

I don't have a problem. I just pointed out that the OP did not ask about any error, so I was a bit a confused about what your replies were about. If I was rude about it, I apologize. I didn't mean to be.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.