Hello David,
You have this nice example of recommended use of class << self as follows:
class Dave
@count = 0
class << self
attr_accessor :count
end
def initialize()
Dave.count += 1
end
end
puts "There are #{Dave.count} instances of Dave."
d1 = Dave.new
d2 = Dave.new
puts "There are #{Dave.count} instances of Dave."
Now following the logic that you explained before this to turn this into self.method syntax, I did the following:
class Dave
@count = 0
self.attr_accessor :count
def initialize()
Dave.count += 1
end
end
puts "There are #{Dave.count} instances of Dave."
d1 = Dave.new
d2 = Dave.new
puts "There are #{Dave.count} instances of Dave."
But when I run this, I get the following error message:
main.rb:3: private method `attr_accessor’ called for Dave:Class (NoMethodError)
I am not sure if I understand what is going on here. Why can’t I use the parallel but clearer syntax that you explained before this.
By the way, brilliant example of include and extend in ActiveRecord setting. You are a really good teacher.
Bharat