So I was doing a refresher on Ruby and I saw this guy's blog about creating class-level instance variable in Ruby. I am still trying to understand what the code actually does here. His blog can be found here
http://railstips.org/blog/archives/2006/11/18/class-and-instance-variables-in-ruby/
and I have created a simple code based on his example to show what I am trying to understand
class Polygon
class << self; attr_accessor :sides end
@sides = 10
def initialize
end
end
class Triangle < Polygon
@sides = 3
class << self; attr_accessor :sides end
def initialize
end
end
puts Triangle.sides #3
puts Polygon.sides #10
So the line that I really want to understand is (probably you guys have guessed it),
class << self; attr_accessor :sides end
What does this really do? what is he appending self to class ? is class an array then? Please elaborate as much as you can. Thank you.