ruby 代码帮助
我是 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']
的所有值
I am beginner in Ruby and having trouble understanding this code
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
In the above code, the statement puts self.data['tags']
(marked 1) outputs more than 10 values as expected. However, the line puts tag
(marked 2) outputs only one value implying that array contains only one value. Isn't self.categories.keys.each
expected to loop through all the values which itself is assigned to self.data['tags']
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在进入循环之前,您可以首先确保“类别”仍然包含许多值:
如果第一个
puts
显示多个值,并且仅调用循环一次,那么您可能需要调查可能导致循环中断的原因。也许write_page
抛出异常,捕获调用堆栈的某个位置?当您调试某些值时,最好使用
inspect
方法而不是(由puts
自动调用)方法to_s
。inspect
的输出更加易于调试。“标签”有可能(尽管不太可能)包含一些控制字符,这些控制字符会清除以前的输出或类似的内容。
inspect
始终是安全的。 (好吧,并非总是如此,但在大多数情况下;)You may start with ensuring that the 'categories' still contain many values before entering the loop:
If the first
puts
shows you more than one value, and the loop is called only once, then you may want to investigate what possibly makes the loop break. Maybewrite_page
throws an exception, catch somewhere up the calling stack?When you debug some values, it's better to use
inspect
method instead of (automagically called byputs
) methodto_s
. The output ofinspect
is more debug-friendly.It is possible, (however unlikely) that the 'tag' contains some control characters which clear the previous output, or something like that. The
inspect
is always safe. (ok, not always but in most of cases ;)