I am a NOOB trying to understand some code.
What does this self.class.current_section do?
class MyClass
class << self
def current_section(*section)
if section.empty?
@current_section
else
@current_section = section[0]
end
end
end
def current_section()
self.class.current_section
end
def current_section=(section)
self.class.current_section(section)
end
end
It forwards the message (method call request) received by the object to the corresponding class.
Say you have a class
Calling
h‘s method, it looks uph‘s class (MyClass) and invokes the methodcurrent_sectionof that class.So, by the definitions above, every object of the class
MyClasshas a methodcurrent_sectionwhich is routed to the centralcurrent_sectionof the class.The definition of the class methods in your question is just using a different syntax for doing the same: adding a method to the class object.