My team has been reading existing ruby code to build our understanding. We commonly find things we don't quite understand, even with research. Please comment on the following questions.
Question 1
For the following code, why is class << self
defined right under the module name? Does that imply defining a class with the module name?
module GravatarImageTag
class << self
attr_accessor :configuration
end
Question 2
Why does the author define class << self
and go on to define methods with self.
? Our understanding is thay you can define self.
methods in a block that starts with class << self
rather than repeat self.
for each method.
module GravatarImageTag
class << self
attr_accessor :configuration
end
def self.configuration
@configuration ||= Configuration.new
end
def self.configure
yield(configuration)
end
Question 3
Why does the author use the class name rather than self in the method self.include(base)
? Furthermore, with this file's structure, what is class is self.include(base)
a member of? This relates to question 1.
module GravatarImageTag
class << self
attr_accessor :configuration
end
def self.configuration
@configuration ||= Configuration.new
end
def self.configure
yield(configuration)
end
class Configuration
attr_accessor :default_image, :filetype, :include_size_attributes,
:rating, :size, :secure
def initialize
@include_size_attributes = true
end
end
def self.included(base)
GravatarImageTag.configure { |c| nil }
base.extend ClassMethods
base.send :include, InstanceMethods
end
Thanks.