0

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?

3
Contributors
6
Replies
26
Views
4 Years
Discussion Span
Last Post by sepp2k
0

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.

Edited by LastMitch: grammer

0

@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.

2

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.

0

What error? No one mentioned any error except you.

What is your problem?

0

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.

Edited by sepp2k

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.