Reputation: 410
I dont understand what an instance variable inside a class method does. Below code makes no sense to me. How can a class method manipulate an instance variable? I can call a class method without even having an instance.
def self.schema
return @schema if @schema
DB.table_info(table) do |row|
@schema[row['name']] = row['type']
@schema
end
Edit: follow up question after @Aetherus's answer.
What is first_name
in the below code example? Why cant i access it as a class variable since it is inside class scope? Why does all 3 methods give me errors?
class Person
#class scope
first_name = "jimmy"
# expected error beacuse it would treat as local variable
def print_name
#instance scope
first_name
end
#to my understanding this should work, but it doesnt. i dont know why
def print_name_2
#instance scope
#with self.class i should be able to access class scope?
self.class.first_name
end
#to my understading this should work, but it doesnt. I dont know why.
def self.print_name
#class scope
#inside class scope, i should able to access first_name?
first_name
end
end
Upvotes: 3
Views: 127
Reputation: 8898
In short, these instance variables belong to the class, not the instances of that class.
To understand it, you need to know more about Ruby.
In Ruby, all the classes are just objects of type Class
.
String.class #=> Class
Array.class #=> Class
Class.class #=> Class
And you can define anonymous classes by instantiating Class
foo = Class.new do
# Here you can define methods
end
foo.new #=> an instance of an anonymous class
Because classes are objects, they can have instance variables too.
There are 4 keywords that triggers scope switching: module
, class
, def
and do
(for blocks). Here I only show the class
and def
.
# in the scope of main
class Foo
# in the scope of Foo
def self.bar
# still in the scope of Foo
end
# in the scope of Foo
def bar
# in the scope of an instance of Foo
end
# back to the scope of Foo again
end
# back to the scope of main
The instance variables defined in a scope belongs to the current object (a.k.a. self
) of that scope. In the previous example, if you define a instance variable in the scope of Foo
, that instance variable belongs to the self
of that scope, which is the class Foo
.
Upvotes: 2