我是 Ruby 的初学者,无法理解这段代码
require_relative 'custom_page'
module Jekyll
class Tag < CustomPage
def initialize(site, base, dir, tag)
super site, base, dir, 'tag'
self.data['tag'] = tag
self.data['title'] = "#{site.config['tag_title_prefix'] || 'Tag: '}#{tag}"
self.data['description'] = "#{site.config['tag_meta_description_prefix'] || 'Tag: '}#{tag}"
end
end
class Tags < CustomPage
def initialize(site, base, dir)
super site, base, dir, 'tags'
self.data['tags'] = site.categories.keys.sort
#1# puts self.data['tags']
end
end
class Site
# generate_tags_categories is called by the custom process function in site_process.rb
def generate_tags_categories
dir = self.config['tag_dir'] || 'tags'
write_page Tags.new(self, self.source, dir) if self.layouts.key? 'tags'
self.categories.keys.each do |tag|
puts "dd"
#2# puts tag
write_page Tag.new(self, self.source, File.join(dir, tag.slugize), tag)
end
end
end
end
在上面的代码中,语句puts self.data['tags']
(标记为 1)按预期输出了 10 多个值。但是,行puts tag
(标记为 2)仅输出一个值,这意味着该数组仅包含一个值。不应self.categories.keys.each
循环遍历本身分配给的所有值self.data['tags']